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
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.
It's showing connect time = 0 and total time = 130 sec, I am not really sure what it means. 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!
Sometimes curl request comes to the REST API Server and sometimes it doesn't. It is discarded at connection level itself, does not reach to the REST API server. I am little confused as to why sometimes it connects and sometimes it doesn't.
To set a timeout for a Curl command, you can use the --connect-timeout parameter to set the maximum time in seconds that you allow Curl to connect to the server, or the --max-time (or -m) parameter for the total time in seconds that you authorize the whole operation.
The first thing to do is to update your WordPress to the latest version if not already done. Then if the problem is still there, contact your hosting company and ask the hosting support team to check the following points : Make sure your server is running a recent version of PHP and the cURL library.
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. 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.
Hope my experience with this issue help someone and save their time to troubleshoot.
Thanks, everyone for commenting and suggesting the solutions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With