note 109377 added to function.curl-multi-exec
| From: | DanielGZylberberg | Date: | Wed, 11 Jul 2012 18:00:55 +0000 |
| Subject: | note 109377 added to function.curl-multi-exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189105@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.
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.";
?>
----
Server IP: 201.216.232.240
Probable Submitter: 190.245.29.113 (proxied: 190.245.29.113)
----
Manual Page -- http://www.php.net/manual/en/function.curl-multi-exec.php
Edit -- https://master.php.net/note/edit/109377
Del: integrated -- https://master.php.net/note/delete/109377/integrated
Del: useless -- https://master.php.net/note/delete/109377/useless
Del: bad code -- https://master.php.net/note/delete/109377/bad+code
Del: spam -- https://master.php.net/note/delete/109377/spam
Del: non-english -- https://master.php.net/note/delete/109377/non-english
Del: in docs -- https://master.php.net/note/delete/109377/in+docs
Del: other reasons-- https://master.php.net/note/delete/109377
Reject -- https://master.php.net/note/reject/109377
Search -- https://master.php.net/manage/user-notes.php