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.

Community
  • 1
  • 1
Irfan.gwb
  • 668
  • 2
  • 13
  • 35
  • 2
    Try to add timeout setting in curl: curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds – Munish Chechi Jun 13 '18 at 12:08
  • 2
    Are there https option for the api? Sometimes nasty ISP act as midleman and tampering with request content to serve some ads. – Charis Jun 21 '18 at 07:27
  • 2
    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. – Irfan.gwb Jun 21 '18 at 07:33
  • 2
    use CURLOPT_VERBOSE to troubleshoot it – hanshenrik Jun 21 '18 at 10:20
  • 2
    because it happen randomly and not because of processing time. But @hanshenrik 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. – Charis Jun 22 '18 at 06:02
  • 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? – DeadLock Jun 26 '18 at 10:50

2 Answers2

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.

Cid
  • 14,968
  • 4
  • 30
  • 45
  • 1
    `[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? – Irfan.gwb Jun 13 '18 at 12:24
  • 1
    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? – Irfan.gwb Jun 13 '18 at 12:27
  • 1
    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. – Cid Jun 13 '18 at 12:28
  • 1
    But do you think it will solve my problem, i think it will throw the time error little sooner (about after 42 sec)? – Irfan.gwb Jun 13 '18 at 12:36
  • 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 timeout – Cid Jun 13 '18 at 14:13
  • 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. – Irfan.gwb Jun 14 '18 at 06:43
  • 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_time` – Cid Jun 14 '18 at 07:11
  • 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. – Irfan.gwb Jun 14 '18 at 12:34
  • 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. – Irfan.gwb Jun 14 '18 at 12:36
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.

Irfan.gwb
  • 668
  • 2
  • 13
  • 35