Cla*_*key 7 php mysql http-status-codes fsockopen get-headers
我正在尝试制作一个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;
?>
Run Code Online (Sandbox Code Playgroud)
您可以尝试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);
Run Code Online (Sandbox Code Playgroud)
更新
看这个例子.http://www.codediesel.com/php/parallel-curl-execution/
| 归档时间: |
|
| 查看次数: |
15334 次 |
| 最近记录: |