PHP curl Connection timed out error
Asked Answered
I

2

12

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!

Edit

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.

Inadvertent answered 13/6, 2018 at 12:2 Comment(6)
Try to add timeout setting in curl: curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in secondsBaywood
Are there https option for the api? Sometimes nasty ISP act as midleman and tampering with request content to serve some ads.Witching
No, Right now no https option for it. How much you sure about it? I can purchase and install SSL if it really means in this case.Inadvertent
use CURLOPT_VERBOSE to troubleshoot itHelico
because it happen randomly and not because of processing time. But @Helico suggestion worth a shot, try to grab as much error message as verbosely as possible. And before any purchase is made, I will prefer using letsencrypt or another free solution first.Witching
Is api-domain.xom a proper domain? Or is it just an entry in your /etc/hosts that you are using temporarily as a domain name mapped to an IP?Kittle
I
1

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.

Inadvertent answered 29/6, 2018 at 5:46 Comment(0)
G
6

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.

You can redefine the timeouts using curl_setopt(). In example, curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 42); to set 42 seconds of connection timeout or curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 0); for no connection timeout. The same way, curl_setopt($cHandler, CURLOPT_TIMEOUT, 42); will define 42 seconds of execution timeout.

Gemmule answered 13/6, 2018 at 12:14 Comment(9)
[total_time] => 130.335916 is it mean that script is waited 130.335916 sec to get the api response and then throw the connection timeout error?Inadvertent
it means we are able to connect to the api-domain.com quickly, but API taking time to processed and return the response, am I correct? is adding curl_setopt($cHandler, CURLOPT_CONNECTTIMEOUT, 42); will fix my problem?Inadvertent
Yes you are correct. You can redefine the execution timeout using CURLOPT_TIMEOUT in curl_setopt(). I'm editing my answer to add this information.Gemmule
But do you think it will solve my problem, i think it will throw the time error little sooner (about after 42 sec)?Inadvertent
Your error is server-side, it looks like it's taking too much time to compute your query. You can either try to optimize your REST API or increase the execution timeoutGemmule
Are you sure about REST API taking time process request and it's not the connection issue with the api-domain.com? And what is the parameter to judge it.Inadvertent
I suggest you to log server-side the time difference between the reception of the request on the API and sending of the answer and compare it with total_timeGemmule
I debug it little more and found that it is taking time in connection itself not in processing, I did same thing as you suggested processing request taking milliseconds.Inadvertent
Some time curl request comes to the REST API Server and sometime not. It discarded at connection level itself, does not reach to the REST API sever. I am little strange why some time it connect and sometime not.Inadvertent
I
1

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.

Inadvertent answered 29/6, 2018 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.