Skip to content

Instantly share code, notes, and snippets.

@lcherone
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/67039196dadafacf33c7 to your computer and use it in GitHub Desktop.
Save lcherone/67039196dadafacf33c7 to your computer and use it in GitHub Desktop.
Curl multi function GET snippet
<?php
function curl_multi($urls = array()) {
$curl = array();
$result = array();
$mh = curl_multi_init();
foreach ($urls as $id=>$url) {
$curl[$id] = curl_init();
curl_setopt($curl[$id], CURLOPT_URL, $url);
curl_setopt($curl[$id], CURLOPT_HEADER, 0);
curl_setopt($curl[$id], CURLOPT_TIMEOUT, 30);
curl_setopt($curl[$id], CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; curl)');
curl_setopt($curl[$id], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $curl[$id]);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
foreach($curl as $id=>$c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);
return $result;
}
$urls[] = 'http://api.url.a.com/';
$urls[] = 'http://api.url.b.com/';
echo '<pre>'.print_r(curl_multi($urls), true).'</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment