检查网站是否可用最快的方式 [英] Check if website is available in fastest possible way

查看:99
本文介绍了检查网站是否可用最快的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach($links as $link_content)
{
  $handle = curl_init(LINK_BASE.$link_content);
  curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
  $response = curl_exec($handle);
  $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  if($httpCode != 200)
    continue; //if not, go to next link
}

检查它们是否可用(返回HTTP代码200)。目前我使用上面写的代码。不幸的是,这个操作需要很长时间 - 超过2-4分钟。我必须以最快的方式检查每个链接。你可以给我任何建议吗?

I need to analyse 350 links and check if each of them is available (returns HTTP code 200). Currently I use the code written above. Unfortunately, this operation takes a really long time - more than 2-4 minutes. I have to check each link in the fastest possible way. Can you give me any suggestions?

推荐答案

我会说你只是使用 CURLOPT_NOBODY ,而不是像你目前一样拉入整个内容。代码如下:

I would say that you simply issue HTTP HEAD requests using CURLOPT_NOBODY rather than pull in the entire content as you are currently doing. The code for this would look like:

foreach($links as $link_content)
{
  $handle = curl_init(LINK_BASE.$link_content);
  curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($handle,  CURLOPT_NOBODY, TRUE); // make HEAD
  $response = curl_exec($handle);
  $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  if($httpCode != 200)
    continue; //if not, go to next link
}

根据需要,您还可以使用 curl_multi 功能来并行化您的请求。您可以随意查看一个简单的基于curl的REST类,我已经创建了一些更好的示例如何做 curl_multi 。欢迎您自由使用课程 - https://github.com/mikecbrant/php-rest-客户

If this doesn't reduce time as much as desired, you can also look into using curl_multi functionality to parallelize your requests. You can feel free to have a look at a simple curl-based REST class I have created to get some better examples on how to do curl_multi. You are welcome to use the class freely - https://github.com/mikecbrant/php-rest-client

这篇关于检查网站是否可用最快的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆