Home > Back-end >  PHP: CURL not showing any response after 10 minute or more time
PHP: CURL not showing any response after 10 minute or more time

Time:12-26

When I'm trying to send data on a API using CURL. API taking long time (10 minute or more) to the execution and at the end API successfully executed on its destination. but After the 10 minute, curl not returning any response.

But same process are complete in under the 5 minutes, then everything is working fine.

Here is my code

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt_array($ch, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "postvar1=value1",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/x-www-form-urlencoded",
  ),
));
$api_res = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$api_obj = json_decode($api_res, true);

Please suggest me the better solution for successfully run this script after 10 minute or more time.

CodePudding user response:

There could be a few reasons why CURL is not returning a response

  1. Check the URL that you're sending the request.
  2. Make sure you're setting the correct options for your request, such as the CURLOPT_TIMEOUT options to set timeouts for the request. The number of seconds to wait while trying to connect.
  3. Make sure that the server where your PHP script is running has a stable network connection and that there are no firewall rules blocking the request.

If none of these suggestions help, you may need to further debug the issue by adding more logging or tracing to your code to see what's happening behind the scenes.

Or if you use postman it has helpful tools that generate CURL for PHP developers

  1. Open postman and try to put the information of your request to make sure the request returns a response successfully.
  2. on the right top, Click on code button.
  3. Select “cURL” from the dropdown.
  4. You can copy the command using Copy to Clipboard button.

I hope this tutorial from postman website will help you Here

CodePudding user response:

I would first test PHP itself, to see if it can run for 10 minutes or longer. Your question doesn't tell us whether you tested this.

You could use this little script:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$start = time();
while (time()   (10 * 60) > $start) {
    1   2   3;
}

echo "I am still here!";

If your PHP can run for 10 minutes or more, you should see the: "I am still here!", message, otherwise it will post the error: "Fatal error: Maximum execution time of ?? seconds exceeded.".

You cannot use sleep() for this, because that will actually pause execution, and therefore stops the execution time timer.

Tips on how to extend the execution time of PHP can be found everywhere.

If PHP can run for 10 minutes or longer then you now know that the problem could indeed be your CURL code, or the API server.

CodePudding user response:

Use this...

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'API_URL',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>'{
            "Param1": 'value1',
            "Param2": 'value2'
        }',
        CURLOPT_HTTPHEADER => array(
            'Authorization: Auth_KEY_if_you_have',
            'Content-Type: application/x-www-form-urlencoded'
        ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
  • Related