Backend Development 6 min read

Using PHP cURL to Send Concurrent Requests to Multiple URLs

This article explains how to enable the PHP cURL extension, create an array of target URLs, and use curl_multi functions with example code to perform concurrent HTTP requests and collect their responses efficiently.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP cURL to Send Concurrent Requests to Multiple URLs

Fetching multiple URLs simultaneously is a common requirement in web development, and the PHP cURL library provides a convenient way to achieve this.

cURL is an open‑source network transfer tool that supports various protocols; PHP’s cURL extension exposes its functionality through a set of functions for sending HTTP requests and retrieving responses.

Before using cURL, ensure the PHP environment has the cURL extension enabled. This can be done by uncommenting the line ;extension=curl in the php.ini file.

Example of enabling the extension:

<code>;extension=curl</code>

Next, create an array containing all URLs you wish to request:

<code>$urls = [
    'http://example.com/api/1',
    'http://example.com/api/2',
    'http://example.com/api/3',
];</code>

Then, use the multi‑cURL functions to send the requests concurrently. The code initializes a multi handle, creates individual handles for each URL, sets the necessary options, adds them to the multi handle, executes all requests, gathers the responses, and finally cleans up all handles.

<code>// Create a multi‑handle
$mh = curl_multi_init();

// Array to store individual handles
$handles = [];

foreach ($urls as $url) {
    // Create a new cURL handle
    $handle = curl_init();

    // Set options
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);

    // Add the handle to the multi‑handle
    curl_multi_add_handle($mh, $handle);

    // Store handle for later reference
    $handles[] = $handle;
}

// Execute all requests
$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running > 0);

// Collect responses
$responses = [];
foreach ($handles as $handle) {
    $response = curl_multi_getcontent($handle);
    $responses[] = $response;

    // Remove and close the handle
    curl_multi_remove_handle($mh, $handle);
    curl_close($handle);
}

// Close the multi‑handle
curl_multi_close($mh);

// Output each response
foreach ($responses as $response) {
    echo $response . "\n";
}
</code>

Code analysis:

Use curl_multi_init() to create a multi‑handle for managing multiple requests.

For each URL, curl_init() creates a new handle, and CURLOPT_URL and CURLOPT_RETURNTRANSFER are set to specify the target and to return the response as a string.

curl_multi_add_handle() adds each individual handle to the multi‑handle.

curl_multi_exec() runs all added requests until they are complete.

curl_multi_getcontent() retrieves the response data for each handle.

After processing, curl_multi_remove_handle() and curl_close() clean up each handle, and curl_multi_close() shuts down the multi‑handle.

Finally, the responses are printed using echo .

By using these functions, developers can perform concurrent HTTP requests efficiently, which is especially useful for web scraping, API aggregation, or any scenario that requires fetching data from multiple sources at once.

Backend DevelopmentPHPcurlWeb ScrapingConcurrent Requests
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.