自从我将服务器从PHP5.3.x升级到PHP5.5.3之后,我遇到了PHP web服务客户端的问题。
我有这样一个函数,用于计时cURL执行请求所需的时间:
function makeRequest($method, $uri) {
global $requests;
addTime("makeRequest");
$curl = curl_init($uri);
curl_setopt($curl, CURLOPT_HEADER , true );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($curl, CURLOPT_MAXREDIRS , 10 );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST , $method);
//////////////////////////////
addTime("curl_exec");
$responseContent = curl_exec($curl);
remTime("curl_exec");
addTime("curl_getInfo");
$responseInfo = curl_getinfo($curl);
remTime("curl_getInfo");
array_push( $requests, var_export( $responseInfo, true ) );
remTime("makeRequest");
}addTime和remTime是记录microtime(true)以精确测量方法执行情况的函数。我还记录了curl_getinfo报告的不同时间)。见下文。
我用了一堆不同的乌里斯。以下是我的研究结果:
https://api1.mywebservice.com/methodCall
makeRequest时间: 0.402
https://api2.mywebservice.com/methodCall
api2.mywebservice.com被硬编码到机器的%windir%\system32\drivers\etc\hosts文件到127.0.0.1,因此它永远不应该命中DNS服务器。
makeRequest时间: 0.402
https://localhost/methodCall
makeRequest时间: 0.403
https://127.0.01/methodCall
makeRequest时间: 0.006
服务器的DNS服务器只有大约5ms的距离,但是无论如何应该缓存名称。api2.mywebservice.com获得相同时间的事实表明,这不是DNS服务器超时的问题,而是cURL内部的问题。
我也尝试过CURLOPT_RESOLVE,但没有效果:
curl_setopt( $c, CURLOPT_RESOLVE, array("api1.mywebservice.com:443:127.0.01") );为什么cURL是最新的(当然也是最伟大的?)PHP版本需要这么长时间才能检索到结果?
发布于 2013-09-21 15:08:56
看起来你的libcurl是用来使用它自己的名字解析器,而不是系统名解析器。在这种情况下,它根本不使用主机文件。
有三种可能的解决办法:
此外,:来自php.net的php5.5.4窗口二进制文件使用系统解析器来实现curl。它使用内部缓存(当使用CURLOPT_DNS_USE_GLOBAL_CACHE时)和系统DNS缓存。我已经用一些测试检查过了。还通过curl检查了名称解析的速度--它与localhost相同(用于在主机文件中写入的任何记录)。所以,我绝对相信这不是PHP问题:
https://127.0.01/methodCall中有一个错误一定是https://127.0.0.1/methodCall。在这种情况下,curl总是返回total_time =0。curl_getinfo($curl)的所有字段,特别是"primary_ip"。https://stackoverflow.com/questions/18888101
复制相似问题