Yeak
Yeak

Reputation: 2538

PHP cURL request takes up the entire Timeout

I currently have a Laravel application, which is doing a CURL request from one route to another route within the same route. My CURL looks like this:

//LOGGING THAT A CURL CALL IS ABOUT TO BE MADE 
$url = env('APP_URL') . '/tests/add/results'; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return server error
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLINFO_HEADER_OUT, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $test_post_data);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);

In the route, that the POST is being sent to, the first thing i log, is the request data:

//LOGING THAT I RECEIVED THE CURL CALL in receiving function

I'm noticing, that the logs for the request data get logged exactly the same amount of time as the timeout, meaning the request is actually being sent 10 seconds after the initial call.

In my logs i'll see something like:

10:10:10 - LOGGING CURL CALL
10:10:20 - Recieving CURL call

If i change the timeout to 30, then the log shows 30 seconds later that i received the CURL call.

Does anyone have any idea why this may be happening?

The response from the CURL just comes back as false always.

Upvotes: 1

Views: 5119

Answers (2)

Yeak
Yeak

Reputation: 2538

I did the following to make a post request work: Instead of calling the route via CURL i did a post directly to the route function

            $testController = new TestsController;
            $test_data_request = new \Illuminate\Http\Request();
            $test_data_request->setMethod('POST');
            $test_data_request->request->add( $test_post_data );
            $testId = $testController->addTestResults($test_data_request);

Upvotes: 2

CodiMech25
CodiMech25

Reputation: 446

You've not provided enough information, but i think, the problem will be one or more of the following:

  • The WebServer http://127.0.0.1:8000 is not running
  • The script located on http://127.0.0.1:8000/tests/add/results is running too long and the request timeouts before it is completed
  • The requested path is returning redirect headers and creates an infinite loop
  • The response is too big to finish the data transfer in thirty seconds (very wierd if on localhost)

Try some more debugging and provide more information on this, so we may help you.


PS: Firstly i would try to catch the headers (curl_setopt($ch, CURLINFO_HEADER_OUT, true);) and print out the response (var_dump($response);) - or save it to some file :)

Upvotes: 0

Related Questions