This example will create a persistent cURL share handle and demonstrate
sharing connections between them. If this is executed in a long-lived
PHP SAPI, $sh
will survive between SAPI requests.
<?php
// Create or retrieve a persistent cURL share handle set to share DNS lookups and connections.
$sh = curl_share_init([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);
// Close the cURL handles
curl_close($ch1);
curl_close($ch2);
?>