简体   繁体   English

PHP比cURL快?

[英]PHP faster than cURL?

what is the fastest way to get the http status code. 获取http状态代码的最快方法是什么。 I have a list within about 10k URL's to check. 我有一个大约10k URL的列表要检查。 And in best case it checks them every 15 minutes. 在最好的情况下,它每15分钟检查一次。 So i've a php script what uses simple curl functions and loop through them all. 所以我有一个php脚本,它使用简单的curl函数并循环遍历它们。 But it takes way too much time. 但这需要花费太多时间。 Any suggestions what i can do to improve that? 有什么建议我可以做些什么来改善它? What about parallel checks on multiple urls? 如何对多个网址进行并行检查? how many could php manage? 有多少人可以管理? I'm very new to this whole performance thing. 我对这整个表演事物都很陌生。

This is what i have: 这就是我所拥有的:

public function getHttpStatus(array $list) {
    $list = array(…); // Array contains 10k+ urls from database.
    for($i = 0; $i < count($list); $i++) {
            $ch = $list[$i];
        curl_setopt($ch, CURLOPT_NOBODY, 1); 
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 5);

        $c = curl_exec($ch); 
        $info = curl_getinfo($ch);
        echo $info['http_code'] . '<br />';
    }
}

Thanks in advance! 提前致谢!

You might consider using curl_multi_exec() - http://php.net/manual/en/function.curl-multi-exec.php , which allows you to process multiple curl handles in parallel. 您可以考虑使用curl_multi_exec() - http://php.net/manual/en/function.curl-multi-exec.php ,它允许您并行处理多个卷曲句柄。 If you like, you can take a look at using a very lightweight REST client I wrote which supports curl_multi_exec() . 如果您愿意,可以使用我编写的非常轻量级的REST客户端来支持curl_multi_exec() The link is here: 链接在这里:

https://github.com/mikecbrant/php-rest-client https://github.com/mikecbrant/php-rest-client

Now, I didn't set up this library to work with HEAD requests, which would actually be much more efficient than GET requests if you are only looking for response codes. 现在,我没有设置此库来处理HEAD请求,如果您只查找响应代码,这实际上比GET请求更有效。 But this should be relatively easy to modify to support such a use case. 但是这应该相对容易修改以支持这样的用例。

At the very least this REST client library can give you good sample code with regards to how to work with curl_multi_exec() 至少这个REST客户端库可以为您提供有关如何使用curl_multi_exec()良好示例代码

Obviously, you would need to play around with the number of concurrent requests that you should use based on what your available hardware and the services you are making requests against can handle. 显然,您需要根据可用硬件和要求处理的服务可以处理的并发请求数量来处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM