Capturing the contents of multiple curl pens from curl multi exec?

So, basically I need to get the contents of about 100-200 web pages. I would like to use curl_multi_ * to take all of this right away, but I'm not sure if this is possible.

I know, with curl, you just set returntransfer to true and print the execution, but how would you do with curl_multi _ *?

If this is not possible, is there any other way to do this?

+5
source share
2 answers

I stumbled upon this, trying to do the same thing myself. I decided that I would add my solution to help anyone who has the same problem in the future. First, I assume that you have an array of your curl handlers as such:

$mh = curl_multi_init();
$requests = array();
foreach ($someArray as $identifier => $url) {
    $requests[$identifier] = curl_init($url);
    curl_setopt($requests[$identifier], CURLOPT_RETURNTRANSFER, true);
    //any other options you need to set go here
    curl_multi_add_handle($mh, $requests[$identifier]);
}

, :

do {
    $status = curl_multi_exec($mh, $running);
} while ($status === CURLM_CALL_MULTI_PERFORM || $running);

, :

$returned = array();
foreach ($requests as $identifier => $request) {
    $returned[$identifier] = curl_multi_getcontent($request);
    curl_multi_remove_handle($mh, $request); //assuming we're being responsible about our resource management
    curl_close($request); //being responsible again.  THIS MUST GO AFTER curl_multi_getcontent();
}

$returned .

+4

curl.

curl_multi .

, , , .

PHP, (googled with 'curl multiple').

-1

All Articles