When using PHP for high concurrent network requests, the overhead of HTTPS handshake is often one of the performance bottlenecks. Each HTTPS request requires a full TLS handshake process, which greatly affects the overall response speed when multiple requests are initiated in a short period of time. Fortunately, cURL provides curl_share_init function, which can multiplex connection data between multiple requests by sharing handles, thereby reducing the frequency of HTTPS handshakes and improving efficiency.
curl_share_init is a function in the cURL extension that is used to initialize a shared handle. Through this shared handle, different cURL sessions can share information such as DNS cache, SSL session, cookies, etc. This means that if multiple requests point to the same domain name and shared SSL sessions are enabled, the requests can reuse the previous HTTPS handshake, greatly reducing latency and CPU consumption.
Here is a practical example code that uses curl_share_init to share an HTTPS session:
<?php
// Initialize the shared handle
$sh = curl_share_init();
// Set sharing options,Open SSL Session Sharing
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
// Prepare multiple requests
$urls = [
'https://m66.net/api/endpoint1',
'https://m66.net/api/endpoint2',
'https://m66.net/api/endpoint3',
];
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); // App Shared Handle
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch) . "\n";
} else {
echo "Response from {$url}:\n$response\n\n";
}
curl_close($ch);
}
// Clean up shared handles
curl_share_close($sh);
?>
In this example, all three requests point to different interfaces under the m66.net domain name, and all share the SSL session through the same shared handle. This means that after completing the HTTPS handshake for the first request, subsequent requests can reuse existing sessions, thereby avoiding the repeated handshake process.
Thread safety : Although PHP itself is single-threaded, if you use a shared handle in a multi-threaded environment, you need to pay attention to thread synchronization issues.
Use CURLSHOPT_UNSHARE : If you need to unshare a certain type of data (such as a cookie or DNS cache) at a certain stage, you can use CURLSHOPT_UNSHARE .
Only reuse of the same host is effective : a shared SSL session can only take effect between the same host (Host) and the port, and cannot be shared across domain names.
By using curl_share_init and shared handle configurations reasonably, we can effectively reduce the system overhead of TLS handshake when making large numbers of HTTPS requests. This has significant advantages in microservice communication, high-frequency API requests, data crawling and other scenarios. Not only improves performance, but also reduces the resource consumption of servers and clients, it is an important skill in PHP high-performance network programming.