Current Location: Home> Latest Articles> Minimum runnable example using curl_share_init() in PHP

Minimum runnable example using curl_share_init() in PHP

M66 2025-05-18

In PHP, the curl_share_init() function is used to initialize a shared cURL handle, which can share certain resources, such as cookies or caches in multiple cURL sessions. This function is especially useful, and can effectively reduce unnecessary resource overhead when you want to reuse certain resources in multiple cURL requests.

Here is a detailed description of how to use the curl_share_init() function, with a minimal runnable example.

1. Introduction to curl_share_init() function

The curl_share_init() function is used to initialize a shared cURL handle. This handle can be used to share resources for some cURL sessions. Using a shared handle can enable multiple cURL sessions to share resources, such as cookies, file handles, etc.

grammar

 $share = curl_share_init();

parameter

  • This function does not accept any parameters and directly returns a shared handle.

Return value

  • Returns a cURL shared handle ( CURLSH type).

  • If initialization fails, false is returned.

2. How to share resources between multiple requests?

Using the curl_share_init() function, you can share resources for multiple cURL sessions. Below we use a minimally runnable example to illustrate how to use this function.

3. Minimum runnable example

In this example, we will show how to create two cURL sessions and use a shared handle to share cookies.

 <?php
// Initialize the shared handle
$share = curl_share_init();

// Initialize the first one cURL Session
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, 'https://m66.net');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

// Convert the shared handle with the first one cURL Session关联
curl_setopt($ch1, CURLOPT_SHARE, $share);

// Execute the first one cURL ask
$response1 = curl_exec($ch1);
if ($response1 === false) {
    echo "cURL Error: " . curl_error($ch1);
}
echo "Response from the first request: " . substr($response1, 0, 100) . "...<br>";

// Initialize the second cURL Session
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, 'https://m66.net');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

// Convert the shared handle with the second cURL Session关联
curl_setopt($ch2, CURLOPT_SHARE, $share);

// Execute the second cURL ask
$response2 = curl_exec($ch2);
if ($response2 === false) {
    echo "cURL Error: " . curl_error($ch2);
}
echo "Response from the second request: " . substr($response2, 0, 100) . "...<br>";

// closure cURL Session
curl_close($ch1);
curl_close($ch2);

// closure共享句柄
curl_share_close($share);
?>

Code description

  1. Initialize the shared handle :
    Use curl_share_init() to initialize a shared handle that will be used for shared resources (such as cookies).

  2. Initialize the cURL session :
    We initialize two cURL sessions ( $ch1 and $ch2 ) and set their URL to https://m66.net .

  3. Shared handle :
    Set a shared handle with curl_setopt() and associate it with each cURL session. This means that the same resources (such as cookies) will be shared between the two sessions.

  4. Execute the request :
    Use curl_exec() to execute a cURL request and get the response. If an error occurs during execution, use curl_error() to output the error message.

  5. Close the resource :
    At the end of the script, we close each cURL session and close the shared handle to free the resource.

4. Summary

By using curl_share_init() we are able to share resources between multiple cURL sessions, thereby increasing efficiency and reducing duplicate work. This function is especially suitable for scenarios where the same resource needs to be shared across multiple requests, such as sharing cookies or cached data. With the above sample code, you can learn how to implement this in PHP.

Hope this article helps you! If you have more questions, please visit our website.