skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search