Current Location: Home> Latest Articles> Best practices for using curl_share_init() in a multithreaded PHP environment

Best practices for using curl_share_init() in a multithreaded PHP environment

M66 2025-05-30

curl_share_init() is a function for initializing shared resources. It allows multiple cURL sessions (i.e. multiple curl requests) to share certain resources, such as: DNS cache, cookie data, file descriptors, etc. In this way, multiple concurrent HTTP requests can share the same connection and session data, thereby avoiding the problem of reinitializing the connection for each request and improving efficiency.

2. Why use curl_share_init() ?

  1. Resource Sharing : Multiple cURL sessions can share DNS caches, cookies, SSL sessions, etc., thereby reducing duplicate DNS queries or cookie sending operations.

  2. Performance optimization : Shared connection and session information reduces resource consumption per request and avoids duplicate network connection establishment processes.

  3. Support for concurrent requests : curl_share_init() can help realize resource sharing during multi-threaded concurrent requests, making multi-threaded requests more efficient.

3. Implement curl_share_init() application in PHP

Suppose we are developing a PHP script that performs multiple HTTP request operations and hope to use curl_share_init() to achieve resource sharing. Here is a basic implementation step:

  1. Initialize shared resources :
    Use curl_share_init() to initialize shared resources and share them with multiple cURL sessions.

  2. Set up shared resources for each request :
    In each cURL request, use curl_share_setopt() to bind the shared resource to the session.

  3. Execute the request :
    Use curl_multi_exec() to execute multiple cURL requests in parallel.

  4. Clean up resources :
    After the request is completed, the shared resource and cURL session are released.

4. Sample code

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

// Set up shared resources,like DNS cache,cookie wait
curl_share_setopt($share, CURLSHOPT_SHARE, CURLSHARE_SSL);

$urls = [
    "http://m66.net/api/endpoint1",
    "http://m66.net/api/endpoint2",
    "http://m66.net/api/endpoint3",
];

// create cURL Multiple session handles
$multi_handle = curl_multi_init();
$curl_handles = [];

foreach ($urls as $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SHARE, $share); // Set up shared resources
    curl_multi_add_handle($multi_handle, $ch);
    $curl_handles[] = $ch;
}

// Perform all cURL ask
$running = null;
do {
    curl_multi_exec($multi_handle, $running);
    curl_multi_select($multi_handle);
} while ($running > 0);

// 获取每个ask的响应内容
foreach ($curl_handles as $ch) {
    $response = curl_multi_getcontent($ch);
    echo "Response: " . $response . "\n";
    curl_multi_remove_handle($multi_handle, $ch);
    curl_close($ch);
}

// Clean up shared resources and cURL Session
curl_share_close($share);
curl_multi_close($multi_handle);
?>

5. Interpretation of key steps

  1. Shared resource initialization : Use curl_share_init() to create a shared resource handle. Then use curl_share_setopt() to set the resource type shared by the shared resource handle (for example: SSL connection, cookies, DNS cache, etc.).

  2. Setting up shared resources : In each cURL session, set each session to share the previously initialized shared resources through curl_setopt($ch, CURLOPT_SHARE, $share) .

  3. Concurrent request execution : Use curl_multi_exec() and curl_multi_select() to process multiple HTTP requests in parallel. curl_multi_exec() will execute all requests, while curl_multi_select() is used to wait for the request to complete.

  4. Response processing and cleaning : After all requests are executed, the response content of each request is obtained through curl_multi_getcontent() , and clean up operations are performed to free resources.

6. Performance optimization and precautions

  1. Reduce the overhead of connection creation : Each cURL session shares the same connection pool, thus significantly reducing the overhead of re-establishing a connection every request, especially when there are a large number of concurrent requests, with performance improvements particularly.

  2. Thread safety issues : curl_share_init() and related operations support sharing of resources in a multi-threaded environment, but it is still necessary to ensure that concurrent access is properly managed to avoid thread conflicts. Generally speaking, sharing between multiple cURL sessions is thread-safe.

  3. Memory and resource management : In the case of a large number of requests, ensure that shared resources and cURL sessions are cleaned up in time after the request is completed to avoid memory leakage.

7. Summary

In a multi-threaded PHP environment, the curl_share_init() function is an important tool for achieving efficient resource sharing and performance optimization. By rationally using shared resources, duplicate connection establishment and resource waste can be avoided, and the efficiency of concurrent requests can be improved. For applications that require frequent HTTP requests, especially in high concurrency scenarios, the sharing mechanism of cURL can effectively improve performance and response speed.