Backend Development 4 min read

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

This article explains the purpose and usage of PHP's curl_multi_getcontent() function, demonstrates how to combine it with curl_multi_init() and curl_multi_exec() for concurrent requests, and provides a complete example showing initialization, execution, content retrieval, and cleanup of 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 over the network. cURL (Client URL) is a powerful PHP extension library for network communication. cURL provides a series of functions, one of which is curl_multi_getcontent() , used to retrieve the content of a cURL session.

The curl_multi_getcontent() function obtains the content of a cURL session created by curl_multi_init() . When using curl_multi_exec() to execute multiple cURL sessions, we can call curl_multi_getcontent() to get each session's return result. The call is simple: just pass a cURL resource handle as the argument.

Below is an example code using curl_multi_getcontent() :

// 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 sessions
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;
}

// Close the multi‑handle sessions
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 corresponding session has finished executing; otherwise the content may not be retrieved correctly.

In summary, curl_multi_getcontent() is a very useful function for obtaining the content of multiple cURL sessions. When making concurrent requests to several APIs, it allows you to fetch each session's response conveniently for further processing.

backendhttpPHPcurlConcurrent Requestscurl_multi_getcontent
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.