简体   繁体   English

PHP curl连接超时错误

[英]PHP curl Connection timed out error

I am calling an API using curl in PHP, Sometimes it works fine and sometimes I get Failed to connect to api-domain.com port 80: Connection timed out 我在PHP中使用curl调用API,有时工作正常,有时Failed to connect to api-domain.com port 80: Connection timed out

It's a little strange that sometimes it's working and sometimes it's not. 有时工作正常,有时却无效,这有点奇怪。 To troubleshoot the issue I have printed the curl_getinfo() when it is not working, please check it below. 要解决此问题,我在curl_getinfo()无法正常工作时将其打印出来,请在下面进行检查。

It's showing connect time = 0 and total time = 130 sec, I am not really sure what it means. 它显示连接时间= 0,总时间= 130秒,我不太确定这是什么意思。 If any one has good understanding about it please review the below log and help me understand what the exact issue is. 如果有人对此有很好的了解,请查看以下日志并帮助我了解确切的问题。

[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

Thank in advance! 预先感谢!

Edit 编辑

Sometimes curl request comes to the REST API Server and sometimes it doesn't. 有时curl请求到达REST API服务器,有时不是。 It is discarded at connection level itself, does not reach to the REST API server. 它会在连接级别本身被丢弃,不会到达REST API服务器。 I am little confused as to why sometimes it connects and sometimes it doesn't. 我对为什么有时连接有时不连接感到困惑。

Refering to the documentation of curl_getinfo() , connect_time is the time in second it took to establish last connection and total_time is the time in second for the last transaction. 参考curl_getinfo()的文档, connect_time是建立最后连接所total_time的时间(秒), total_time是最后一次total_time的时间(秒)。

You can redefine the timeouts using curl_setopt() . 您可以使用curl_setopt()重新定义超时。 In example, curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 42); 例如, curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 42); to set 42 seconds of connection timeout or curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 0); 设置42秒的连接超时或curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 0); for no connection timeout. 没有连接超时。 The same way, curl_setopt($cHandler, CURLOPT_TIMEOUT, 42); 同样的方式, curl_setopt($cHandler, CURLOPT_TIMEOUT, 42); will define 42 seconds of execution timeout. 将定义42秒的执行超时。

After struggling around 1 week and trying all suggested options finally I found that it was neither a connection issue nor API api-domain.com issue. 经过大约1周的苦苦挣扎,并尝试了所有建议的选项,最终我发现这既不是连接问题也不是API api-domain.com问题。 Little strange for me but true that it's my SERVER scaling issue from which I am calling the API, I have increased the configuration (RAM and CPU) and found that the issue has been fixed. 对我来说有点奇怪,但是确实这是我调用API时遇到的SERVER扩展问题,我增加了配置(RAM和CPU)并发现此问题已得到解决。

Hope my experience with this issue help someone and save their time to troubleshoot. 希望我在此问题上的经验能帮助某人并节省他们进行故障排除的时间。

Thanks, everyone for commenting and suggesting the solutions. 谢谢大家对我们的解决方案提出意见和建议。

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

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