PHP provides the curl_share_init function, which enables sharing certain types of data between the Curl handles. By using a shared handle, multiple requests can reuse resources such as DNS lookups or active connections. This can improve performance and reduce overhead when making repeated or related HTTP requests.
This example demonstrates how to use a shared Curl handle to allow multiple requests to reuse DNS lookup results and existing connections. First, a share handle is created with curl_share_init, and its options are configured to share both DNS and connection data. Each request-specific handle ($ch1 and $ch2) is then associated with this shared handle using CURLOPT_SHARE. When the first request is executed, it performs a full DNS resolution and establishes a connection. The second request, using the same shared resources, can reuse that connection rather than creating a new one, reducing overhead and improving performance for repeated calls to the same host.
<?php
// Share handle used to share DNS lookups and connections
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
// Initialize the first handle and attach the share handle to it
$ch1 = curl_init('https://httpbin.org/get');
curl_setopt($ch1, CURLOPT_SHARE, $sh);
// Execute the first handle
curl_exec($ch1);
// Initialize the second handle and attach the share handle to it
$ch2 = curl_init('https://httpbin.org/get');
curl_setopt($ch2, CURLOPT_SHARE, $sh);
// Execute the second handle. This may reuse the connection from the first handle
curl_exec($ch2);
However, the curl_share_init function can only share data between handles within a single PHP execution cycle.
Since PHP 8.5, we can use the curl_share_init_persistent function, which enables data sharing between handles created in different PHP requests. Persistent share handles created with curl_share_init_persistent are not destroyed at the end of the request. If another request initializes a persistent share handle with the same sharing options, the existing one will be reused, reducing overhead and avoiding the repeated cost of initializing handles.
This example uses curl_share_init_persistent, which allows handles to share DNS lookups and connections across multiple PHP requests - not just within the same execution cycle, as with curl_share_init. Because persistent share handles are stored and reused between requests, the first handle may benefit from work done in a previous request, and subsequent handles in the same script can reuse the connection established by earlier ones. This reduces repeated DNS resolutions and connection setup overhead, leading to improved performance for applications making frequent or repeated HTTP calls.
<?php
// Persistent share handle used to share DNS lookups and connections
$sh = curl_share_init_persistent([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT]);
// Initialize the first handle and attach the share handle to it
$ch1 = curl_init('https://httpbin.org/get');
curl_setopt($ch1, CURLOPT_SHARE, $sh);
// Execute the first handle. This may reuse the connection from a previous PHP request
curl_exec($ch1);
// Initialize the second handle and attach the share handle to it
$ch2 = curl_init('https://httpbin.org/get');
curl_setopt($ch2, CURLOPT_SHARE, $sh);
// Execute the second handle. This may reuse the connection from the first handle
curl_exec($ch2);
Leave a Comment
Cancel reply