Created
May 5, 2015 19:46
-
-
Save jbafford/93d99758764691f59f59 to your computer and use it in GitHub Desktop.
Curl Multi example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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