Home > Software engineering >  PHP CURL : Loop until HTTP 200 Response is received
PHP CURL : Loop until HTTP 200 Response is received

Time:03-23

I'm sending a Post Data to a website, but that website return's 502 Bad Gateway most of the time. I want to send Post data every second until that Website accepts my Post Request.

<?php
$postData = "";
$req = curl_init("example.com");
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $postData);
curl_setopt($req, CURLOPT_HTTPHEADER, array(");
curl_setopt($req, CURLOPT_COOKIE,"\_");
$result = curl_exec($req);

echo "Status code: ".curl_getinfo($req, CURLINFO_HTTP_CODE)."\\n";
echo "Response body: ".$result."\\n";
curl_close($req);

CodePudding user response:

There are many ways to repeat doing the curl every n seconds. You may loop over the curl block with sleep, or simply use HTML refresh to do the job.

For sleep, you may refer to the following documentation:

https://www.php.net/manual/zh/function.sleep.php

Option 1: For HTML way, you may simply add a line (say to re-run the curl script every 5 seconds):

<meta http-equiv="refresh" content="5">

PHP code can be:

<meta http-equiv="refresh" content="5">

<?php
$postData = "";
$req = curl_init("example.com");
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $postData);
curl_setopt($req, CURLOPT_HTTPHEADER, array(");
curl_setopt($req, CURLOPT_COOKIE,"\_");
$result = curl_exec($req);

echo "Status code: ".curl_getinfo($req, CURLINFO_HTTP_CODE)."\\n";
echo "Response body: ".$result."\\n";
curl_close($req);
?>

Option 2: For PHP way (or command mode), please do things thru sleep like this:

<?php

$success=0;

while ($success==0){

// your original curl

$postData = "";
$req = curl_init("example.com");
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $postData);
curl_setopt($req, CURLOPT_HTTPHEADER, array(");
curl_setopt($req, CURLOPT_COOKIE,"\_");
$result = curl_exec($req);

echo "Status code: ".curl_getinfo($req, CURLINFO_HTTP_CODE)."\\n";

if ( curl_getinfo($req, CURLINFO_HTTP_CODE) =="200" ){
$success=1; 
}

echo "Response body: ".$result."\\n";
curl_close($req);

// end curl


     // wait for 10 seconds before the next iteration
        sleep(10); 
     // end wait.
}
?>

However (for option 2), please set the php script execution timeout to be long enough for your need. Say by amending the php.ini , or add the following to the top of your PHP:

ini_set('max_execution_time', 5000); //5000 seconds

CodePudding user response:

Try this

$processCompleted = false;
$interval = 5; //5 seconds
$request = curl_init('http://www.example.com/');
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:'.$token));

while(!$processCompleted) {
    
    $response = curl_exec($request);
    // Check HTTP status code
    if (!curl_errno($response)) {
        switch ($http_code = curl_getinfo($request , CURLINFO_HTTP_CODE)) {
        case 200:  # OK
            ...//do your stuff
            break;
        default:
            sleep($interval);
        }
    }
}
curl_close($ch);

CodePudding user response:

You just need to repeat your curl_exec call until CURLINFO_RESPONSE_CODE is 200.

$postData = "";
$req = curl_init("example.com");
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $postData);
curl_setopt($req, CURLOPT_HTTPHEADER, array(''));
curl_setopt($req, CURLOPT_COOKIE,'\_');

do {
    $result = curl_exec($req);
    $responseCode = curl_getinfo($req, CURLINFO_RESPONSE_CODE);

    echo "Status code: $responseCode\n";
    echo "Response body: $result\n";

    // keep in mind that sleep returns 0 on success,
    // that would evaluate to false without strict comparison
} while ($responseCode !== 200 && sleep(1) !== false);

curl_close($req);
  • Related