我正在尝试编写一个 PHP 脚本来尽快检查网站的 HTTP 状态。
我目前正在使用 get_headers() 并在来自 mysql 数据库的 200 个随机 url 的循环中运行它。
检查所有 200 个 - 平均需要 2 分钟 48 秒。
我能做些什么来让它(大大)更快吗?
(我知道 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 同时发送多个请求
例子:
$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);
已更新
看这个例子。 http://www.codediesel.com/php/parallel-curl-execution/
关于php - 比 get_headers() 更快的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9964223/