この例では、持続的な cURL 共有ハンドルを作成し、
接続を共有する方法を示します。
このコードを長く生き残る PHP SAPI で実行した場合、
$sh
は SAPI リクエスト間で生き残ります。
<?php
// Create or retrieve a persistent cURL share handle set to share DNS lookups and connections
$sh = curl_share_init_persistent([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT]);
// Initialize the first cURL handle and assign the share handle to it
$ch1 = curl_init("http://example.com/");
curl_setopt($ch1, CURLOPT_SHARE, $sh);
// Execute the first cURL handle. This may reuse the connection from an earlier SAPI request
curl_exec($ch1);
// Initialize the second cURL handle and assign the share handle to it
$ch2 = curl_init("http://example.com/");
curl_setopt($ch2, CURLOPT_SHARE, $sh);
// Execute the second cURL handle. This will reuse the connection from $ch1
curl_exec($ch2);
?>