using curl_multi, have loaded 3 url's , printed array.
1) how can set timestamp on output let me know when each url run?
2) how can explode array display data , timestamp, excluding text "array ( [0] =>", "[1] =>", "[2] =>" , " )"?
code
<?php function multirequest($data, $options = array()) { // array of curl handles $curly = array(); // data returned $result = array(); // multi handle $mh = curl_multi_init(); // loop through $data , create curl handles // add them multi-handle foreach ($data $id => $d) { $curly[$id] = curl_init(); $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; curl_setopt($curly[$id], curlopt_url, $url); curl_setopt($curly[$id], curlopt_header, 0); curl_setopt($curly[$id], curlopt_returntransfer, 1); // post? if (is_array($d)) { if (!empty($d['post'])) { curl_setopt($curly[$id], curlopt_post, 1); curl_setopt($curly[$id], curlopt_postfields, $d['post']); } } // options? if (!empty($options)) { curl_setopt_array($curly[$id], $options); } curl_multi_add_handle($mh, $curly[$id]); } // execute handles $running = null; { curl_multi_exec($mh, $running); } while($running > 0); // content , remove handles foreach($curly $id => $c) { $result[$id] = curl_multi_getcontent($c); curl_multi_remove_handle($mh, $c); } // done curl_multi_close($mh); return $result; } $data = array(array(),array()); $data[0]['url'] = 'http://search.yahooapis.com/videosearchservice/v1/videosearch?appid=yahoodemo&query=pearl+jam&output=json'; $data[1]['url'] = 'http://search.yahooapis.com/videosearchservice/v1/videosearch?appid=yahoodemo&query=black+eyed+peas&output=json'; $data[2]['url'] = 'http://search.yahooapis.com/videosearchservice/v1/videosearch?appid=yahoodemo&query=nirvana&output=json'; $r = multirequest($data); print_r($r); ?>
output
array ( [0] => pearl jam <br> [1] => black eyed peas<br> [2] => nirvana )
preferred output
01:00:01 pearl jam <br> 01:00:02 black eyed peas<br> 01:00:03 nirvana
i'd keep simple...just loop set timestamp , single curl call, explode resultarray , filter out key or values don't need.
for curl call:
$r= array(); foreach($urls $url) { $r[]["timestamp"]= time(); $output= curl_exec($url); $r[]["bandname"]= $output; }
your final loop like
foreach($r $key=>$value){ print $value["timestamp"]; print $value["bandname"]; print "<br>"; }
Comments
Post a Comment