比get_headers()更快的东西

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)

saf*_*rov 8

您可以尝试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/

  • 您可以使用multi-curl一次完成所有200个请求.只需最慢的服务器就可以响应.如果其中一个需要60秒,则整个请求将花费60秒.但是你可以在curl中设置超时. (4认同)