In PHP development, frequent call to the RESTful interface is a very common scenario, especially in microservice architectures, communication between different services often depends on HTTP requests. cURL is the most commonly used extension in PHP to handle HTTP requests. Although the performance overhead of a single request is usually acceptable, in high concurrency scenarios, if each request repeatedly initializes the connection resource, it will cause a considerable performance bottleneck.
At this time, the curl_share_init() function comes in handy. It provides a resource sharing mechanism that allows multiple cURL handles to share information such as DNS cache, SSL sessions, etc., thereby reducing the time for connection establishment and thus improving the efficiency of overall interface requests.
curl_share_init() is a resource sharing initialization function provided by cURL, which returns a cURL Share handle. With this handle, you can configure multiple cURL requests to share specific resources, for example:
DNS cache ( CURLSHOPT_SHARE and CURL_LOCK_DATA_DNS )
Cookies ( CURLSHOPT_SHARE and CURL_LOCK_DATA_COOKIE )
SSL sessions ( CURLSHOPT_SHARE and CURL_LOCK_DATA_SSL_SESSION )
These shared data can significantly reduce repeated calculations and handshakes per request.
Suppose we need to request multiple service interfaces concurrently, for example:
https://api.m66.net/service1
https://api.m66.net/service2
https://api.m66.net/service3
Here is a complete PHP example using curl_share_init() to implement resource sharing:
<?php
// Initialize the shared handle
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
// To request URL Array
$urls = [
'https://api.m66.net/service1',
'https://api.m66.net/service2',
'https://api.m66.net/service3',
];
// initialization curl_multi Handle
$mh = curl_multi_init();
$handles = [];
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SHARE, $sh); // Set up shared resources
curl_multi_add_handle($mh, $ch);
$handles[] = $ch;
}
// Perform concurrent requests
$running = null;
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh);
} while ($running > 0);
// Get results
foreach ($handles as $ch) {
$response = curl_multi_getcontent($ch);
echo "Response: " . $response . "\n";
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}
// Clean up resources
curl_share_close($sh);
curl_multi_close($mh);
?>
The benefits of using curl_share_init() are mainly reflected in the following aspects:
DNS cache sharing : Avoid repeated domain name resolution for each request, especially when multiple requests access the same domain name, the benefits are obvious.
SSL session reuse : Reduce SSL handshake overhead, especially save CPU resources in scenarios where HTTPS interface calls are frequent.
Improved connection efficiency : When used with HTTP Keep-Alive, it can greatly improve interface request efficiency.
In high concurrency environments or application scenarios where multiple interfaces are frequently called, rational use of curl_share_init() can significantly improve the overall performance and response speed of the system.
Not all resources are suitable for sharing, especially cookies, which may cause security or state pollution issues.
The shared resource must be closed after all cURL handles that have been used by the shared resource are closed.
Not all PHP installations have enabled cURL shared support, and you can check the support status through curl_version() .