0

I have a cURL code which was working before but recently it stopped working and gives a 408 request timeout error. I want to know the reason why it stopped working and how can I fix it. Here's the code :

$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlSession, CURLOPT_URL, $actualUrl); 
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlSession, CURLOPT_POST, 0);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlSession, CURLOPT_TIMEOUT,1100);
curl_setopt($curlSession, CURLOPT_SSLCERT, $clientcert); 
curl_setopt($curlSession, CURLOPT_SSLCERTTYPE, 'PEM'); 
curl_setopt($curlSession, CURLOPT_SSLKEYTYPE, 'PEM'); 
curl_setopt($curlSession, CURLOPT_SSLKEY, $keyfile); 
curl_setopt($curlSession, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curlSession, CURLOPT_USERAGENT, $agent);
$data = curl_exec($curlSession);

I tried it with the Postman application and it still works on Postman without any delay.Is there any alternative to cURL that I can use to check this?

Saeesh Tendulkar
  • 516
  • 3
  • 18

1 Answers1

0

The 408 Request Timeout error is an HTTP status code that means the request you sent to the website server (e.g. a request to load a web page) took longer than the website's server was prepared to wait. In other words, your connection with the website "timed out."

You can test your API with postman first to see whether it is working fine.

Is your api return large amount of data or your remote server is slow resulting a long request-response time?

See documentation: http://www.php.net/manual/en/function.curl-setopt.php

CURLOPT_CONNECTTIMEOUT - The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.

CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds

also don't forget to enlarge time execution of php script if needed.

set_time_limit(0);
KARASZI István
  • 28,974
  • 8
  • 95
  • 116
Tanvir
  • 101
  • 6
  • Yeah. I'm sorry I forgot to mention that. I tried it with Postman and it works. The data isn't so large and it used to work before. I just wanted to know why it stopped. – Saeesh Tendulkar Jan 01 '19 at 08:41
  • I tried it with 1000 timeout. Is there any alternative to cURL in php that I can check with. – Saeesh Tendulkar Jan 01 '19 at 08:50
  • can you test the api from server using terminal or any other tools.When your working stable software suddenly stop working, more often it is not an issue in your code rather than server side or network issue.Also for an alternative of CURL you can see this [post](https://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents). – Tanvir Jan 02 '19 at 06:25
  • That's what I want to know. What is the server or network issue that happened suddenly, so I can fix it. – Saeesh Tendulkar Jan 02 '19 at 06:28