Backend Development 4 min read

Using curl_multi_getcontent() to Retrieve Content from Multiple cURL Sessions in PHP

This article explains how the PHP curl_multi_getcontent() function works, demonstrates its usage together with curl_multi_init() and curl_multi_exec() for concurrent requests, provides a complete code example, and highlights important considerations when fetching results from multiple cURL handles.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using curl_multi_getcontent() to Retrieve Content from Multiple cURL Sessions in PHP

In PHP development we often need to request data from other servers, and the cURL extension provides powerful network communication functions. One of these functions is curl_multi_getcontent() , which retrieves the content of a single cURL session.

The purpose of curl_multi_getcontent() is to obtain the response of a handle created by curl_multi_init() after the handles have been executed with curl_multi_exec() . The function simply takes a cURL resource handle as its argument and returns the fetched data.

Below is a complete example that shows how to use curl_multi_getcontent() :

// Initialize two cURL sessions
$ch1 = curl_init('http://www.example.com/api1');
$ch2 = curl_init('http://www.example.com/api2');

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

// Add the two sessions to the multi‑handle
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);

// Execute the multi‑handle
do {
    $status = curl_multi_exec($mh, $active);
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);

// Retrieve the content of each session
$contents = array();
foreach([$ch1, $ch2] as $ch) {
    $content = curl_multi_getcontent($ch);
    $contents[] = $content;
}

// Remove the handles and close the multi‑handle
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

// Output the fetched contents
var_dump($contents);

In the code above we first initialize two cURL sessions with curl_init() and add them to a multi‑handle created by curl_multi_init() . We then run the sessions using curl_multi_exec() . While the multi‑handle is active we call curl_multi_getcontent() for each handle to collect the responses into an array, and finally we clean up with curl_multi_remove_handle() and curl_multi_close() .

It is important to ensure that a session has finished executing before calling curl_multi_getcontent() ; otherwise the function may return an empty result.

In summary, curl_multi_getcontent() is a very useful function for obtaining the output of multiple concurrent cURL requests, making it easier to handle several API calls in parallel.

PHP Practical Development Rapid Entry

Scan the QR code to receive free learning materials

BackendHTTPPHPNetworkingcurlMulti
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.