Backend Development 4 min read

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

This article explains how the PHP curl_multi_getcontent() function retrieves the response content of each handle in a multi‑cURL session, demonstrates its usage with a complete example, and highlights important considerations for concurrent API requests.

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

In PHP development we often need to request data from other servers, and cURL (Client URL) is a powerful extension for network communication; the curl_multi_getcontent() function is used to obtain the content of a cURL session.

The curl_multi_getcontent() function retrieves the content of sessions created by curl_multi_init() . When executing multiple sessions with curl_multi_exec() , you can call curl_multi_getcontent() to get each session's result by passing the cURL handle resource.

Below is an example code that uses the curl_multi_getcontent() function:

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

// Create a new 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);

// Loop to retrieve each session's content
$contents = array();
foreach([$ch1, $ch2] as $ch) {
    $content = curl_multi_getcontent($ch);
    $contents[] = $content;
}

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

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

In the code above, we first use curl_init() to initialize two cURL sessions and add them to a multi‑handle. Then we execute the sessions with curl_multi_exec() . During execution we call curl_multi_getcontent() to fetch each session's content and store it in an array. Finally we clean up with curl_multi_remove_handle() and curl_multi_close() .

Note that before calling curl_multi_getcontent() you must ensure the session has finished executing; otherwise the content may not be retrieved correctly.

In summary, the curl_multi_getcontent() function is very useful for obtaining the results of multiple concurrent cURL requests, making it easier to process responses from several APIs at once.

php8, here I come

Scan the QR code to receive free learning materials

backendhttpPHPAPIcurlmulti-curl
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.