最近在对另外一个公司接口进行调用时,发现日志中有大量超时日志,但对方排查却反映他们的接口处理时间都在100ms以下。于是准备对接口调用进行详细记录一下,尤其是接口响应超时,即没有返回http状态的时候,curl请求的info到底是个什么样。在接口调用时增加curl_error($ch)和curl_errno($ch)捕获错误信息最后发现,信息如下:

{
    "url": "http://www.04007.cn/#示例URL",
    "content_type": null,
    "http_code": 0,
    "header_size": 0,
    "request_size": 0,
    "filetime": -1,
    "ssl_verify_result": 0,
    "redirect_count": 0,
    "total_time": 0,
    "namelookup_time": 0,
    "connect_time": 0,
    "pretransfer_time": 0,
    "size_upload": 0,
    "size_download": 0,
    "speed_download": 0,
    "speed_upload": 0,
    "download_content_length": -1,
    "upload_content_length": -1,
    "starttransfer_time": 0,
    "redirect_time": 0,
    "redirect_url": "",
    "primary_ip": "",
    "certinfo": [],
    "http_error": "name lookup timed out",
    "http_errno": 6
}
    可见报错信息:name lookup timed out 错误码为6.网上也看了看相关的资料,这个问题有多种原因。

有一种原因是IPV6的问题:如果服务器支持IPV6的话,由于curl先解析IPV6,再解析IPV4域名。会造成超时。可在代码中设置curl强制访问为IPv4
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
不过这个方法在我这里行不通,我也进行了尝试,同时也查看服务器未支持IPV6.我们直接使用指host解决。
# vim /etc/hosts 
当然修改服务器的DNS也可能能解决。
# vim /etc/resolv.conf 

相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!