Current Location: Home> Latest Articles> Explanation of CURLSHOPT_UNSHARE Usage: How to Correctly Use curl_share_init to Cancel Shared Resources?

Explanation of CURLSHOPT_UNSHARE Usage: How to Correctly Use curl_share_init to Cancel Shared Resources?

M66 2025-07-08

In multithreaded or concurrent request environments, CURL allows different cURL sessions to share certain resources. For example, using the curl_share_init function to create a shared handle and configuring multiple cURL requests to share connection pools or cookies using curl_setopt. CURLSHOPT_UNSHARE is an option related to the shared handle used to cancel resource sharing between cURL sessions.

The purpose of CURLSHOPT_UNSHARE is to instruct cURL to stop using shared resources. It can be applied to different types of resources, such as cookies, DNS, SSL, etc. This option allows you to cancel sharing in multiple requests, enabling each request to run independently.

How to Use curl_share_init and CURLSHOPT_UNSHARE?

First, we need to initialize a shared resource handle using the curl_share_init function. Then, we configure the shared resource using curl_setopt, and finally, cancel the sharing using CURLSHOPT_UNSHARE.

Step 1: Initialize Shared Resources

// Initialize a shared handle  
$sh = curl_share_init();  
<p>// Set the type of shared resource (e.g., sharing cookies)<br>
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);<br>

In this step, we create a shared handle $sh and set the shared resource type. For example, here we choose to share cookies (CURL_LOCK_DATA_COOKIE).

Step 2: Configure Multiple cURL Sessions to Share Resources

// Initialize cURL session 1  
$ch1 = curl_init();  
curl_setopt($ch1, CURLOPT_URL, 'http://m66.net/page1');  
curl_setopt($ch1, CURLOPT_SHARE, $sh);  
<p>// Initialize cURL session 2<br>
$ch2 = curl_init();<br>
curl_setopt($ch2, CURLOPT_URL, '<a rel="noopener" target="_new" class="" href="http://m66.net/page2">http://m66.net/page2</a>');<br>
curl_setopt($ch2, CURLOPT_SHARE, $sh);</p>
<p>// Execute requests<br>
curl_exec($ch1);<br>
curl_exec($ch2);<br>

In this step, we create two cURL sessions $ch1 and $ch2, and use curl_setopt($ch, CURLOPT_SHARE, $sh) to share resources (e.g., cookies) from $sh. At this point, both requests share the same cookie.

Step 3: Use CURLSHOPT_UNSHARE to Cancel Sharing

If, for some reason, we need to cancel sharing, we can use CURLSHOPT_UNSHARE.

// Cancel sharing cookies  
curl_share_setopt($sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE);  
<p>// Create a new cURL session with independent resources<br>
$ch3 = curl_init();<br>
curl_setopt($ch3, CURLOPT_URL, '<a rel="noopener" target="_new" class="" href="http://m66.net/page3">http://m66.net/page3</a>');<br>
curl_exec($ch3);<br>

In this step, we use curl_share_setopt($sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE) to cancel shared cookies. The next cURL session (e.g., $ch3) will no longer share cookies with previous sessions.

Considerations When Using CURLSHOPT_UNSHARE

  1. Scope of Canceling Sharing: CURLSHOPT_UNSHARE only cancels the sharing of a specific resource type, such as cookies, DNS, or SSL. You can choose which resource type to cancel sharing based on your needs.

  2. Lifecycle of Shared Handles: Cancelling resource sharing does not destroy the shared handle. If you no longer need the shared handle, you can destroy it using the curl_share_cleanup function.

  3. Impact on Concurrent Requests: In multithreaded or concurrent request scenarios, properly using CURLSHOPT_UNSHARE can prevent unnecessary data sharing between different requests, improving the system's flexibility and security.

Conclusion

CURLSHOPT_UNSHARE is a very useful cURL option that helps us cancel resource sharing between multiple cURL sessions. By properly using curl_share_init and curl_setopt to configure shared resources, we can effectively manage resource usage. In practical applications, deciding whether to cancel resource sharing based on needs can improve the flexibility and performance of programs.