我正在尝试制作一个 PHP 脚本来尽快检查网站的 HTTP 状态。
我目前正在使用 get_headers() 并在 mysql 数据库中的 200 个随机 url 循环中运行它。
检查全部 200 个 - 平均需要 2m 48s。
我能做些什么来让它(更快)更快吗?
(我知道 fsockopen - 它可以在 20 秒内检查 200 个站点上的端口 80 - 但这与请求 http 状态代码不同,因为服务器可能会在端口上响应 - 但可能无法正确加载网站等)
这是代码..
<?php
function get_httpcode($url) {
$headers = get_headers($url, 0);
// Return http status code
return substr($headers[0], 9, 3);
}
###
## Grab task and execute it
###
// Loop through task
while($data = mysql_fetch_assoc($sql)):
$result = get_httpcode('http://'.$data['url']);
echo $data['url'].' = '.$result.'<br/>';
endwhile;
?>
您可以尝试 CURL 库。您可以同时发送多个并行请求CURL_MULTI_EXEC
Example:
$ch = curl_init('http_url');
curl_setopt($ch, CURLOPT_HEADER, 1);
$c = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
print_r($info);
UPDATED
看这个例子。http://www.codediesel.com/php/parallel-curl-execution/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)