Skip to content

Instantly share code, notes, and snippets.

@jbafford
Created May 5, 2015 19:46
Show Gist options
  • Save jbafford/93d99758764691f59f59 to your computer and use it in GitHub Desktop.
Save jbafford/93d99758764691f59f59 to your computer and use it in GitHub Desktop.
Curl Multi example
<?php
function DoCURLMulti($urls, $options, $processFn, &$errors)
{
$ch = array();
$mh = curl_multi_init();
foreach($urls as $urlID => $url)
{
$cr = curl_init();
curl_setopt_array($cr, $options);
curl_setopt($cr, CURLOPT_URL, $url);
curl_multi_add_handle($mh, $cr);
$ch[$urlID] = $cr;
}
do {
do {
$exec = curl_multi_exec($mh, $stillRunning);
} while($exec == CURLM_CALL_MULTI_PERFORM);
$ms = curl_multi_select($mh);
} while($stillRunning);
$urlData = array();
$errors = array();
foreach($ch as $urlID => $cr)
{
$urlData[$urlID] = false;
$ce = curl_error($cr);
if(!$ce)
$urlData[$urlID] = $processFn(curl_multi_getcontent($cr), $errors[$urlID]);
else
$errors[$urlID] = array('curl' => $ce);
curl_multi_remove_handle($mh, $cr);
curl_close($cr);
}
curl_multi_close($mh);
return $urlData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment