Current Location: Home> Latest Articles> How to share resources between multiple cURL requests using curl_share_init()

How to share resources between multiple cURL requests using curl_share_init()

M66 2025-05-18

In PHP, cURL is a very powerful library for sending HTTP requests and getting remote data. Typically, each cURL request needs to be independently initialized and configured, but in some cases, sharing certain resources (such as connections, sessions, etc.) between multiple requests can greatly improve performance. At this time, the curl_share_init() function comes in handy. It can share some resources between multiple cURL requests, thereby reducing the overhead of repeated connection establishment and improving the efficiency of requests.

What is the curl_share_init() function?

The curl_share_init() function is used to initialize a shared resource handle for sharing data between multiple cURL requests. It is usually used with curl_share_setopt() , which can set the shared resource type, such as shared connections, cookies, or DNS, etc.

The basic syntax of this function is as follows:

 $ch = curl_share_init();

The curl_share_init() function returns a shared resource handle that can be used for subsequent sharing settings and multi-request operations.

Why use curl_share_init() ?

  1. Reduce connection overhead : If multiple requests access the same domain name or server, using shared resources can avoid duplicate connection creation and reduce network latency and resource consumption.

  2. Shared session data : For example, shared resources such as cookies, DNS, or file descriptors can prevent each request from processing these content separately and improve request efficiency.

  3. Improve performance : Improves application performance by reducing duplicate connection creation and destruction, especially in the case of large numbers of requests.

How to use curl_share_init() ?

Here is a simple example showing how to use curl_share_init() to share resources among multiple cURL requests to improve request efficiency.

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

// Set sharing options,If shared cookies
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);

// Initialize multiple cURL ask
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://m66.net/api/data1");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_SHARE, $share);  // Share resources

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://m66.net/api/data2");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SHARE, $share);  // Share resources

// 执行ask
$response1 = curl_exec($ch1);
$response2 = curl_exec($ch2);

// 关闭ask
curl_close($ch1);
curl_close($ch2);

// 关闭Share resources
curl_share_close($share);

// Processing response data
echo "Response 1: " . $response1 . "\n";
echo "Response 2: " . $response2 . "\n";
?>

Code parsing

  1. Initialize shared resources : Create a shared resource handle $share by calling curl_share_init() , which is the basis for subsequent shared operations.

  2. Set sharing options : Use curl_share_setopt() to set the shared resource type. In this example, we set the shared cookies data.

  3. Initialize multiple cURL requests : Create two cURL requests for $ch1 and $ch2 , respectively, and set them to access different API interfaces. Through the CURLOPT_SHARE option, tell cURL that both requests need to use the shared resource handle $share .

  4. Execute request : execute two requests separately and store the returned response in $response1 and $response2 .

  5. Close request and shared resources : After all requests are completed, call curl_close() to close the cURL request, and finally call curl_share_close() to close the shared resources.

Common options for curl_share_init()

Here are some common sharing options when setting up a shared resource using curl_share_setopt() :

  • CURL_LOCK_DATA_COOKIE : Share cookies.

  • CURL_LOCK_DATA_DNS : Share DNS data.

  • CURL_LOCK_DATA_SSL_SESSION : Share the SSL session.

Depending on the application scenario, selecting the appropriate sharing option can effectively improve performance.

summary

Through the curl_share_init() function, PHP developers can share resources between multiple cURL requests, thereby reducing duplicate creation of connections and improving request efficiency. In scenarios with high concurrent requests, using shared resources will significantly reduce server load and improve system performance. Therefore, mastering and rationally using curl_share_init() is an important skill to optimize cURL requests.