Current Location: Home> Latest Articles> Use curl_share_init() in CLI scripts to manage multiple API requests

Use curl_share_init() in CLI scripts to manage multiple API requests

M66 2025-05-18

When writing PHP CLI scripts that require a large number of API requests, rational management of resources and improving performance has become issues that developers cannot ignore. Especially in scenarios where multiple HTTP requests are required to process concurrently, some advanced functions provided by cURL, such as curl_share_init() , can help us reuse connections and share resources to a certain extent, thereby improving overall execution efficiency. This article will dive into how to manage multiple requests using curl_share_init() in CLI scripts.

1. What is curl_share_init()?

curl_share_init() is a cURL function provided by PHP to initialize a shared handle. This handle allows you to share DNS cache, SSL sessions, cookies and other resources between multiple cURL sessions.

Supported sharing options include:

  • CURLSHOPT_SHARE :

    • CURL_LOCK_DATA_COOKIE : Share cookie data.

    • CURL_LOCK_DATA_DNS : Shared DNS cache.

    • CURL_LOCK_DATA_SSL_SESSION : Share the SSL session.

These sharing options are especially useful when requesting interfaces of the same host or domain name at high frequency.

2. Basic steps to use curl_share_init()

Let's take a look at an actual PHP CLI script example, which simulates multiple concurrent request APIs and uses curl_share_init() to improve performance.

 <?php

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

// Initialize the shared handle
$sh = curl_share_init();

// Set sharing type
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);

$multiHandle = curl_multi_init();
$curlHandles = [];

foreach ($urls as $url) {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SHARE => $sh,
        CURLOPT_USERAGENT => "PHP-Curl-Client"
    ]);
    curl_multi_add_handle($multiHandle, $ch);
    $curlHandles[] = $ch;
}

// Perform multi-threaded requests
$active = null;
do {
    $mrc = curl_multi_exec($multiHandle, $active);
    curl_multi_select($multiHandle);
} while ($active > 0);

// Processing results
foreach ($curlHandles as $ch) {
    $response = curl_multi_getcontent($ch);
    $info = curl_getinfo($ch);
    echo "URL: " . $info['url'] . "\n";
    echo "HTTP Code: " . $info['http_code'] . "\n";
    echo "Response: " . substr($response, 0, 100) . "...\n\n";

    curl_multi_remove_handle($multiHandle, $ch);
    curl_close($ch);
}

// Clean up resources
curl_share_close($sh);
curl_multi_close($multiHandle);

3. Performance comparison and resource optimization

The benefits of using curl_share_init() are particularly evident in the following aspects:

  1. DNS cache reuse <br> Avoid re-resolving the domain name every time you request, saving DNS query time.

  2. SSL session multiplexing <br> For HTTPS requests, multiplexing SSL sessions can significantly reduce handshake latency.

  3. Cookie Sharing <br> Share login status, tracking information, etc. among multiple requests to improve the consistency and effectiveness of requests.

  4. Memory usage optimization <br> Multiple connections share some resources, which can effectively reduce memory overhead.

4. Suggestions for use

Although curl_share_init() is powerful, not all scenarios are suitable for use. Here are some typical scenarios recommended:

  • Bulk requests multiple interfaces under the same domain name;

  • High-frequency interface synchronization is performed in CLI tools;

  • Backend service scripts, such as crawlers, data collection, etc.

In a web environment (such as in FPM mode), it is not recommended to use a shared handle because the resource cannot be shared safely between concurrent processes, which may lead to conflicts or errors.

Conclusion

By rationally using curl_share_init() , PHP CLI scripts can manage multi-request tasks more efficiently, which not only improves execution speed, but also saves system resources. This is a valuable optimization method for CLI tools that require frequent communication with external APIs. Mastering and applying these advanced cURL technologies will greatly improve your competitiveness when building high-performance PHP scripts.