Skip to content

Instantly share code, notes, and snippets.

@tingplenting
Created December 16, 2015 13:37
Show Gist options
  • Save tingplenting/55fa2c213ba9c430ab01 to your computer and use it in GitHub Desktop.
Save tingplenting/55fa2c213ba9c430ab01 to your computer and use it in GitHub Desktop.
<?php
/**
* Response Code Multi Curl
* @param array $urls list url
* @return array
*/
function checkHttpCode($urls) {
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$mh = curl_multi_init();
$http_code = array();
foreach ($urls as $key => $url) {
$requests[$key] = curl_init($url);
//set opptions
curl_setopt ($requests[$key], CURLOPT_URL,$url);
curl_setopt ($requests[$key], CURLOPT_USERAGENT, $user_agent);
curl_setopt ($requests[$key], CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($requests[$key], CURLOPT_VERBOSE, false);
curl_setopt ($requests[$key], CURLOPT_TIMEOUT, 5);
curl_multi_add_handle($mh,$requests[$key]);
}
//execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
$i = 0;
foreach ($requests as $key => $request) {
curl_multi_remove_handle($mh, $request);
$http_code[] = array(
'response' => curl_getinfo($request, CURLINFO_HTTP_CODE),
'url' => $urls[$i]
);
$i++;
}
curl_multi_close($mh);
$filtered = array_filter($http_code, function($k) {
return $k['response'] == '200';
});
return $filtered;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment