开发者

Grabbing the contents of multiple curl handles from a curl multi exec?

开发者 https://www.devze.com 2023-02-01 17:14 出处:网络
So, basically I need to get the contents of around 100-200 webpages. I\'d like to use curl_multi_* so that I can grab it all at once, but I\'m not sure if that\'s even possible.

So, basically I need to get the contents of around 100-200 webpages. I'd like to use curl_multi_* so that I can grab it all at once, but I'm not sure if that's even possible.

I know with开发者_如何学C curl, you'd just set the returntransfer option to true and output the execution, but how would I do this with curl_multi_*?

If impossible, is there any other way to do this?


I stumbled upon this while trying to do the same thing myself. I figured I'd add my solution to help anybody who has the same problem in the future. First, I'll assume 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]);
}

I also assume you ran the requests:

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

Finally, we get to the answer to your question:

$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 now contains all your data.


It is an easy work to do with curl multi interface.

curl_multi interface provide kind of grouping function.

You initiate a normal curl operation with easy interface, register to multi handle, perform multiple job at once, that's all.

If you work in PHP, take a look at This page (googled with 'curl multiple').

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号