PHP 卷曲缓慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9304645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Curl Slowness
提问by KDV
For some reason my curl call is very slow. Here is the code I used.
出于某种原因,我的 curl 调用非常慢。这是我使用的代码。
$postData = "test"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
Executing this code takes on average 250ms to finish. However when I just open the url in a browser, firebug says it only takes about 80ms.
执行此代码平均需要 250 毫秒才能完成。但是,当我在浏览器中打开 url 时,firebug 说它只需要大约 80 毫秒。
Is there something I am doing wrong? Or is this the overhead associated with PHP Curl.
有什么我做错了吗?或者这是与 PHP Curl 相关的开销。
It's the call to
这是呼吁
curl_exec
That is taking up all the time.
这一直在占用。
UPDATE:
更新:
So I figured out right after I posted this that if I set the curl option
所以我在发布这篇文章后就发现,如果我设置了 curl 选项
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
It significantly slows down
它显着减慢
curl_exec
The post data could be anything and it will slow it down.
发布数据可以是任何东西,它会减慢速度。
Even if I set
即使我设置
curl_setopt($ch, CURLOPT_POST, false);
It's slow.
它很慢。
I'll try to work around it by just adding the parameters to the URI as a query string.
我将尝试通过将参数作为查询字符串添加到 URI 来解决它。
SECOND UPDATE:
第二次更新:
Confirmed that if I just call the URI using GET and passing parameters as a query string it is much faster than using POST and putting the parameters in the body.
确认如果我只是使用 GET 调用 URI 并将参数作为查询字符串传递,它比使用 POST 并将参数放入正文要快得多。
回答by Roman Newaza
CURL has some problems with DNS look-ups. Try using IP address instead of domain name.
CURL 在 DNS 查找方面存在一些问题。尝试使用 IP 地址而不是域名。
回答by Brent Baisley
Curl has the ability to tell exactly how long each piece took and where the slowness is (name lookup, connect, transfer time). Use curl_getinfo (http://www.php.net/manual/en/function.curl-getinfo.php) after you run curl_exec.
Curl 能够准确地告诉每个片段花费了多长时间以及缓慢的位置(名称查找、连接、传输时间)。运行 curl_exec 后使用 curl_getinfo ( http://www.php.net/manual/en/function.curl-getinfo.php)。
If curl is slow, it is generally not the PHP code, it's almost always network related.
如果 curl 很慢,通常不是 PHP 代码,它几乎总是与网络相关。
回答by eagle
try this
尝试这个
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
回答by s1lence
The curl functions in php directly use the curl command line tool under *nix systems.
php中的curl函数直接使用*nix系统下的curl命令行工具。
Therefore it really only depends on the network speed since in general curl itself is much faster than a webbrowser since it (by default) does not load any additional data like included pictures, stylesheets etc. of a website.
因此,它实际上仅取决于网络速度,因为通常 curl 本身比网络浏览器快得多,因为它(默认情况下)不会加载任何其他数据,例如网站的包含图片、样式表等。
It might be possible that you are not aware, that the network performance of the server on which you were testing your php script is way worse than on your local computer where you were testing with the browser. Therefore both measurements are not really comparable.
您可能不知道,您在其上测试 php 脚本的服务器的网络性能比在您使用浏览器进行测试的本地计算机上差得多。因此,这两种测量实际上并没有可比性。
回答by Jaspreet Chahal
generally thats acceptable when you are loading contents or posting to slower end of world. curl call are directly proportional to your network speed and throughput of your webserver
当您加载内容或发布到较慢的世界末日时,这通常是可以接受的。curl 调用与您的网络速度和网络服务器的吞吐量成正比