Comments on: How to use curl_multi() without blocking https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/ building stuff on the web with Josh Fraser Mon, 12 Feb 2018 07:30:00 +0000 hourly 1 By: JohnShroff https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-74701 Mon, 12 Feb 2018 07:30:00 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-74701 Absolutely fantastic and insightful post! I was basing my implementation off of another example and was having problems. When sending 100 simultaneous requests, I was getting back 70-80 right away, then the last 20 or so would stall and eventually come back empty. Batching them like this solved the issue and actually made it much faster overall!

]]>
By: ecatombe https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-74641 Tue, 03 Jan 2017 05:42:00 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-74641 Some problems.

1.- if some result is distint from 200 no add more request.

2.- $options[CURLOPT_URL] = $urls[$i++]; When do you check that there is no more elements in array?, o my god.

]]>
By: Artjom Kurapov https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-74581 Fri, 26 Aug 2016 07:14:00 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-74581 Btw, if someone has problems with PHP, you may look into disabling session blocking for the same user

]]>
By: Oded Arbel https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-74551 Mon, 27 Jun 2016 13:08:00 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-74551 This is a brilliant article and sums very clearly how to use curl_multi_exec() correctly – unlike all the examples on php.net which do a horrible work of that.

I’ve seen many times implementations of curl multi that are based on just a copy&paste of some php.net example code and that completely misses the point of being able to pipe requests through the multi processor. Thanks for writing this!

]]>
By: Elad Karako https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-74511 Wed, 02 Dec 2015 02:37:00 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-74511 In reply to Michael.

just pass it along….

function rolling_curl($urls, $callback, $additional_callback_variables = [], $custom_options = null) {

// make sure the rolling window isn’t greater than the # of urls

$rolling_window = min([5, count($urls)]);

$rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;

$master = curl_multi_init();

$curl_arr = array();

// add additional curl options here

$std_options = array(CURLOPT_RETURNTRANSFER => true,

CURLOPT_FOLLOWLOCATION => true,

CURLOPT_MAXREDIRS => 5);

$options = ($custom_options) ? ($std_options + $custom_options) : $std_options;

// start the first batch of requests

for ($i = 0; $i < $rolling_window; $i++) {

$ch = curl_init();

$options[CURLOPT_URL] = $urls[$i];

curl_setopt_array($ch,$options);

curl_multi_add_handle($master, $ch);

}

do {

while(($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);

if($execrun != CURLM_OK)

break;

// a request was just completed — find out which one

while($done = curl_multi_info_read($master)) {

$info = curl_getinfo($done[‘handle’]);

if ($info[‘http_code’] == 200) {

$output = curl_multi_getcontent($done[‘handle’]);

// request successful. process output using the callback function.

$callback($output, $additional_callback_variables);

// start a new request (it’s important to do this before removing the old one)

$ch = curl_init();

$options[CURLOPT_URL] = $urls[$i++]; // increment i

curl_setopt_array($ch,$options);

curl_multi_add_handle($master, $ch);

// remove the curl handle that just completed

curl_multi_remove_handle($master, $done[‘handle’]);

} else {

// request failed. add error handling.

}

}

} while ($running);

curl_multi_close($master);

return true;

}

]]>
By: eliosh https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-74431 Wed, 26 Aug 2015 17:04:00 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-74431 This is my solution for Multi Head Requests:

https://gist.github.com/anonymous/1a9eb381f6a5f260bd20

]]>
By: Paris Nakita Kejser https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-74391 Tue, 07 Jul 2015 12:20:00 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-74391 i have use this method to handle my images downloader from external partners, i use xdebug so if you use this remember to disable xdebug.max_nesting_level i have make a cap on 1milion and make a inner loop to call next curl instenas, i can pase on our company internetline ( not so good ) like 1500-2000 images in a minut.

Thanks a lot for this guide and its helping a lot! 🙂

]]>
By: ترافل https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-72921 Sun, 01 Mar 2015 10:15:01 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-72921 I like your idea of a rolling window

]]>
By: Mark https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-1/#comment-71511 Fri, 02 Jan 2015 07:35:37 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-71511 In reply to Priit.

if (sizeof($urls) >= $i + 1) {

]]>
By: Jonathan Rodan https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/comment-page-2/#comment-47251 Wed, 06 Aug 2014 23:35:47 +0000 https://onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/#comment-47251 The line
while(($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);

is a CPU killer. You can probably use curl_multi_select instead or suffer the los time of a microsleep().
Kaolin Fire's solution is much better than the line I specified.

]]>