简体   繁体   English

使用PHP进行CURL - 非常慢

[英]CURL with PHP - Very slow

All, 所有,

I have to request a URL which returns a JSON request. 我必须请求一个返回JSON请求的URL。 I am using PHP and CURL to do this. 我正在使用PHP和CURL来做到这一点。 Currently it takes around 3-4 seconds for the request and response. 目前,请求和响应大约需要3-4秒。

Following is the curl code 以下是卷曲代码

    $ch = curl_init();
    $devnull = fopen('/tmp/curlcookie.txt', 'w');

    curl_setopt($ch, CURLOPT_STDERR, $devnull);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $desturl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

    $ret = curl_exec($ch);

    curl_close($ch);

    if ($devnull)
    {
        fclose($devnull);
    }

Following is the CURL_GETINFO array 以下是CURL_GETINFO数组

Array
(
    [url] => https://xx.xx.xxx.xx/portalsite/tester
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 198
    [request_size] => 835
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 2.054561
    [namelookup_time] => 6.5E-5
    [connect_time] => 0.016048
    [pretransfer_time] => 0.123947
    [size_upload] => 699
    [size_download] => 46735
    [speed_download] => 22746
    [speed_upload] => 340
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 1.743973
    [redirect_time] => 0
)

How can I speed up the CURL processing time? 如何加快CURL处理时间?

Thanks 谢谢

看起来大部分时间都在等待服务器响应(starttransfer_time - pretransfer_time = 1.620026)...也许服务器正在做一些需要时间的数据库或其他操作?

According to this answer (similar problem) cURL can be slow if you are on Mac OS X and you access to your project with xxxx.local (with 127.0.0.1 myproject.local in your /etc/hosts/ 根据这个答案 (类似的问题),如果您使用的是Mac OS X,并且使用xxxx.local (在/etc/hosts/使用127.0.0.1 myproject.local访问项目,则cURL可能会很慢

As @lepix said: 正如@lepix所说:

It is because the .local tld is reserved for Bonjour service, and this since Mac OS X Lion (10.7). 这是因为.local tld是为Bonjour服务保留的,这是从Mac OS X Lion(10.7)开始的。

Hope it will help, thanks to lepix. 希望它会有所帮助,感谢lepix。

I had some problem like that, using wget it was fast (1 second max), using cURL it took about 5 seconds to get the page, when tcpdump-ing I have found that cURL try to do a revers DNS lookup, and if the server doesn't have the revers DNS registred it will slow you down, I set up an reverse DNS on my local DNS server so every request to that site using cURL now goes very fast. 我有一些这样的问题,使用wget它很快(最多1秒),使用cURL花了大约5秒钟来获取页面,当tcpdump-ing我发现cURL尝试进行反向DNS查找,如果服务器没有反向DNS注册它会降低你的速度,我在我的本地DNS服务器上设置了反向DNS,因此现在使用cURL对该站点的每个请求都非常快。 I didn't find a way to disable revers DNS lookup from cURL settings. 我没有找到一种方法来禁用cURL设置中的反向DNS查找。

My sugestion is to scan your trafic to see where it is waiting so long. 我的消化是扫描你的交通,看看它在哪里等待这么久。

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

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