note 109377 modified in function.curl-multi-exec by danbrown

From: Date: Mon, 16 Jul 2012 15:36:08 +0000
Subject: note 109377 modified in function.curl-multi-exec by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189189@lists.php.net to get a copy of this message
This function wait for the last page, can get a configuration array with curl options to made a post, or pass timeouts, etc.&#13;&#10;Retun the same array but add &#34;error&#34;, and &#34;data&#34;.Error is the string destription if something fail, data is the response.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;// author: Daniel G Zylberberg&#13;&#10;// date 11 jul 2012 &#13;&#10;// $res: array with structure 0=&#62;array(&#34;url&#34;=&#62;&#34;blah&#34;),1=&#62;array(&#34;url&#34;=&#62;&#34;some url&#34;)&#13;&#10;// $options (optional): array with curl options (timeout, postfields, etc)&#13;&#10;// return the same array that gets, and add &#34;data&#34; to the current row(html content)&#13;&#10;// and &#34;error&#34;, with the string description in the case that something fail.&#13;&#10;&#13;&#10;function multiCurl($res,$options=&#34;&#34;){&#13;&#10;&#13;&#10; if(count($res)&#60;=0) return False; &#13;&#10;&#13;&#10; $handles = array(); &#13;&#10;&#13;&#10; if(!$options) // add default options&#13;&#10; $options = array(&#13;&#10; CURLOPT_HEADER=&#62;0,&#13;&#10; CURLOPT_RETURNTRANSFER=&#62;1,&#13;&#10; );&#13;&#10;&#13;&#10; // add curl options to each handle&#13;&#10; foreach($res as $k=&#62;$row){&#13;&#10; $ch{$k} = curl_init();&#13;&#10; $options[CURLOPT_URL] = $row[&#39;url&#39;];&#13;&#10; curl_setopt_array($ch{$k}, $options);&#13;&#10; $handles[$k] = $ch{$k};&#13;&#10; }&#13;&#10;&#13;&#10; $mh = curl_multi_init(); &#13;&#10;&#13;&#10; foreach($handles as $k =&#62; $handle){&#13;&#10; curl_multi_add_handle($mh,$handle);&#13;&#10; //echo &#34;&#60;br&#62;adding handle {$k}&#34;;&#13;&#10; }&#13;&#10;&#13;&#10; $running_handles = null;&#13;&#10; //execute the handles&#13;&#10; do {&#13;&#10; $status_cme = curl_multi_exec($mh, $running_handles);&#13;&#10; } while ($cme == CURLM_CALL_MULTI_PERFORM);&#13;&#10;&#13;&#10; while ($running_handles &#38;&#38; $status_cme == CURLM_OK) {&#13;&#10; if (curl_multi_select($mh) != -1) { &#13;&#10; do {&#13;&#10; $status_cme = curl_multi_exec($mh, $running_handles);&#13;&#10; // echo &#34;&#60;br&#62;&#39;&#39;threads&#39;&#39; running = {$running_handles}&#34;;&#13;&#10; } while ($status == CURLM_CALL_MULTI_PERFORM);&#13;&#10; }&#13;&#10; }&#13;&#10;&#13;&#10; foreach($res as $k=&#62;$row){&#13;&#10; $res[$k][&#39;error&#39;] = curl_error($handles[$k]);&#13;&#10; if(!empty($res[$k][&#39;error&#39;]))&#13;&#10; $res[$k][&#39;data&#39;] = &#39;&#39;;&#13;&#10; else&#13;&#10; $res[$k][&#39;data&#39;] = curl_multi_getcontent( $handles[$k] ); // get results&#13;&#10;&#13;&#10; // close current handler&#13;&#10; curl_multi_remove_handle($mh, $handles[$k] );&#13;&#10; } &#13;&#10; curl_multi_close($mh);&#13;&#10; return $res; // return response&#13;&#10;} &#13;&#10;&#13;&#10;$res = array(&#13;&#10; &#34;11&#34;=&#62;array(&#34;url&#34;=&#62;&#34;http://localhost/public_html/test/sleep.php?t=1&#34;),&#13;&#10; &#34;13&#34;=&#62;array(&#34;url&#34;=&#62;&#34;http://localhost/public_html/test/sleep.php?t=3&#34;),&#13;&#10; &#34;25&#34;=&#62;array(&#34;url&#34;=&#62;&#34;this doesn&#39;t exist&#34;),&#13;&#10;&#13;&#10;);&#13;&#10;print_r( multiCurl($res));&#13;&#10;?&#62;&#13;&#10;&#13;&#10;---------- sleep.php -------------------------------------&#13;&#10;&#60;?php&#13;&#10;sleep($_GET[&#39;t&#39;]);&#13;&#10;echo &#34;sleep for {$_GET[&#39;t&#39;]} seconds and show this.&#34;;&#13;&#10;?&#62; --was-- This function wait for the last page, can get a configuration array with curl options to made a post, or pass timeouts, etc. Retun the same array but add "error", and "data".Error is the string destription if something fail, data is the response. <? // author: Daniel G Zylberberg // date 11 jul 2012 // $res: array with structure 0=>array("url"=>"blah"),1=>array("url"=>"some url") // $options (optional): array with curl options (timeout, postfields, etc) // return the same array that gets, and add "data" to the current row(html content) // and "error", with the string description in the case that something fail. function multiCurl($res,$options=""){ if(count($res)<=0) return False; $handles = array(); if(!$options) // add default options $options = array( CURLOPT_HEADER=>0, CURLOPT_RETURNTRANSFER=>1, ); // add curl options to each handle foreach($res as $k=>$row){ $ch{$k} = curl_init(); $options[CURLOPT_URL] = $row['url']; curl_setopt_array($ch{$k}, $options); $handles[$k] = $ch{$k}; } $mh = curl_multi_init(); foreach($handles as $k => $handle){ curl_multi_add_handle($mh,$handle); //echo "<br>adding handle {$k}"; } $running_handles = null; //execute the handles do { $status_cme = curl_multi_exec($mh, $running_handles); } while ($cme == CURLM_CALL_MULTI_PERFORM); while ($running_handles && $status_cme == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $status_cme = curl_multi_exec($mh, $running_handles); // echo "<br>''threads'' running = {$running_handles}"; } while ($status == CURLM_CALL_MULTI_PERFORM); } } foreach($res as $k=>$row){ $res[$k]['error'] = curl_error($handles[$k]); if(!empty($res[$k]['error'])) $res[$k]['data'] = ''; else $res[$k]['data'] = curl_multi_getcontent( $handles[$k] ); // get results // close current handler curl_multi_remove_handle($mh, $handles[$k] ); } curl_multi_close($mh); return $res; // return response } $res = array( "11"=>array("url"=>"http://localhost/public_html/test/sleep.php?t=1"), "13"=>array("url"=>"http://localhost/public_html/test/sleep.php?t=3"), "25"=>array("url"=>"this doesn't exist"), ); print_r( multiCurl($res)); ?> ---------- sleep.php ------------------------------------- <? sleep($_GET['t']); echo "sleep for {$_GET['t']} seconds and show this."; ?> http://php.net/manual/en/function.curl-multi-exec.php

« previous php.notes (#189189) next »