When using PHP's cURL library for network requests, we usually create a separate cURL handle ( curl_init() ) to operate. However, curl_share_init() and curl_share_setopt() come in handy when we need to share some data between multiple cURL handles (such as cookies, DNS caches, or SSL sessions, etc.).
This article will analyze in detail how to use these functions and how to implement resource sharing and cancel sharing mechanisms through CURLSHOPT_SHARE and CURLSHOPT_UNSHARE .
In cURL, each cURL handle is independent by default. If you need to have multiple handles share some internal data (such as DNS resolution cache, cookie data, or SSL session cache), you can use the "share handle".
PHP provides the following related functions:
curl_share_init() : Initialize a shared handle.
curl_share_setopt() : Set sharing options for shared handles.
curl_share_close() : Close the shared handle.
Corresponding options:
CURLSHOPT_SHARE : Specifies the resource type to share.
CURLSHOPT_UNSHARE : Specifies the type of resource to be unshared.
Suppose we have two different cURL requests and we want them to share the same cookie data. In this way, the cookie set for the first request and the second request can be used automatically.
<?php
// Initialize the shared handle
$sh = curl_share_init();
// Set up shared content,We share here Cookie
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
// Initialize the first one cURL Handle
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://m66.net/page1");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_SHARE, $sh);
$response1 = curl_exec($ch1);
curl_close($ch1);
// Initialize the second cURL Handle
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://m66.net/page2");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SHARE, $sh);
$response2 = curl_exec($ch2);
curl_close($ch2);
// Cancel Sharing Cookie
curl_share_setopt($sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE);
// 关闭共享Handle
curl_share_close($sh);
// Output result
echo "Response 1:\n$response1\n";
echo "Response 2:\n$response2\n";
?>
1?? curl_share_init()
We first call curl_share_init() to create a shared handle $sh , which is specifically used to manage the data to be shared.
2?? curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE)
Here we specify the shared cookie data. CURL_LOCK_DATA_COOKIE represents cookie data, and other available values include:
CURL_LOCK_DATA_DNS (DNS cache)
CURL_LOCK_DATA_SSL_SESSION (SSL session)
You can call curl_share_setopt() multiple times to share a variety of data.
3?? CURLOPT_SHARE
Each cURL handle ( $ch1 , $ch2 ) must be bound to the shared handle $sh through CURLOPT_SHARE so that they can share the specified data.
4?? curl_share_setopt($sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE)
When sharing a certain data is no longer necessary, you can specify that you cancel sharing with CURLSHOPT_UNSHARE . Note: UNSHARE only cancels sharing of this type of data, but the shared handle can still be used.
5?? curl_share_close($sh)
Finally, release the shared handle resource. Note that this step is necessary, otherwise it may cause memory leaks.
Shared handles are only valid within the same process.
Not all data types are suitable for sharing, and specific scenarios are needed when applying them in actual applications.
Use shared handles in multi-threaded environments to ensure thread safety.