php - 比 get_headers() 更快的东西

标签 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;
?>

最佳答案

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

相关文章:

mysql - 在数据库中引用外键的更好方法

api - 当请求时间过长时使用哪个 HTTP 响应代码?

PHP 获取 HTTP 400 响应的内容

php - 拒绝从浏览器访问公共(public) php 但允许应用程序使用

php - 从 mysql_connect() 获取 PHP PDO 连接?

javascript - 使用 .htpasswd 锁定 .user.js 文件

MySQL从字符串中选择字段

mysql - 使用 'Group By' 和 'WHERE' 的 SQL 查询如果没有数据则返回所有计数为 0 的值

部分成功请求的 HTTP 状态代码

php - 在本地和远程开发环境之间同步数据库