Home > Enterprise >  http post in loop is running timing out
http post in loop is running timing out

Time:02-02

I am trying to send curl request from source to destination in loop. Loop runs for 2 times. First request lasts for 32 seconds and second one for 50 seconds. Finally times out. Controlling timeout is not in my control as it is shared hosting.

Source section below is being run in the browser. the below error message shows after using up 120 seconds

Error Details: Fatal error: Maximum execution time of 120 seconds exceeded

Question I am assuming that the request should not timeout, since both requests are submitted separately through their own curl request. Still, it seems like it is getting consolidated to form total one request.

In case I run the loop for one time, then everything works as it takes 30 seconds.

Am I missing anything?

Source

for($i = 0; $i <= 200; $i = 100) {
    $postData = array(
        'start' => $i,
        'end' => $i   100
    );

    $ch = curl_init('Server url');
    curl_setopt_array($ch, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json'
        ),
        CURLOPT_POSTFIELDS => json_encode($postData)
    ));
    
    $response = curl_exec($ch);
    $responseData = json_decode($response, TRUE);
    curl_close($ch);
    
    echo $response;
}

Destination

public function methodname()
{
    $json = json_decode(file_get_contents('php://input'), true);
    // .
    // .
    // Logic that runs for 32 seconds
    // .
    // . 
    header('Content-type: application/json');
    echo json_encode("message");
}

CodePudding user response:

Try to add a sleep(1) function inner your loop. It could be that the server which you are requested dont like multiple POST request in a short time.

CodePudding user response:

try using cURl's CURLOPT_TIMEOUT or similar configurations. More information https://www.php.net/manual/en/function.curl-setopt.php here

Answer: read the documentation

LE:

You could also use set_time_limit(0); // or value > 120 to increase your script execution timeout

  •  Tags:  
  • Related