PHP curl连接超时错误

Irf*_*gwb 11 php curl php-curl

我在PHP中使用curl调用API,有时它工作正常,有时我得到 Failed to connect to api-domain.com port 80: Connection timed out

这有点奇怪,有时它正在工作,有时它不是.要解决我打印的问题,curl_getinfo()当它不工作时,请在下面查看.

它显示连接时间= 0和总时间= 130秒,我不确定它是什么意思.如果任何人对此有充分了解,请查看以下日志并帮助我了解具体问题.

[url] => http://api-domain.com/?act=get_story_banners
[content_type] => text/html; charset=UTF-8
[http_code] => 200
[header_size] => 630
[req   uest_size] => 283
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 130.335916
[namelookup_time] => 0.000016
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 744
[speed_download] => 13814
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] => 
[primary_ip] => 34.231.133.7
[certinfo] => Array()
[primary_port] => 80
[local_ip] => xxx.xxx.xxx.xxx
[local_port] => 48080
Run Code Online (Sandbox Code Playgroud)

预先感谢!

编辑

有时卷曲请求来自REST API服务器,有时它不会.它在连接级别本身被丢弃,不会到达REST API服务器.我有点困惑,为什么有时它连接,有时它不连接.

Cid*_*Cid 6

参考curl_getinfo()的文档,connect_time是建立最后一次连接total_time所花费的时间,是最后一次事务的秒数.

您可以使用curl_setopt()重新定义超时.例如,curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 42);设置42秒的连接超时或curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 0);没有连接超时.同样的方法,curl_setopt($cHandler, CURLOPT_TIMEOUT, 42);将定义42秒的执行超时.


Irf*_*gwb 1

经过大约 1 周的努力并尝试了所有建议的选项后,我终于发现这既不是连接问题,也不是 API api-domain.com 问题。对我来说有点奇怪,但事实是,这是我调用 API 的服务器扩展问题,我增加了配置(RAM 和 CPU),发现问题已得到解决。

Hope my experience with this issue help someone and save their time to troubleshoot.

Thanks, everyone for commenting and suggesting the solutions.