How to do an if or while in php for time out in the API

Asked

Viewed 84 times

1

Well, When the api takes a time out it jumps the Cpf that needs to query to validate the data and goes to the next one, I would like when the time out error happened it stayed in loop until it validates the data and then go to the next one, this is the error that gives: Error:Failed to connect to SITE port 80: Timed out

<?php
set_time_limit(0);
date_default_timezone_set('America/Sao_Paulo');
error_reporting(0);

function GetStr($str, $start, $end)
{
    $a = explode($end, explode($start, $str)[1] )[0];

    return $a;
}

if (isset($_POST['lista'])) {

    foreach (explode("\r\n", $_POST['lista']) as $idade) {
        flush();
        ob_flush();
        
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'URL');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,  'acao=consultar%20idade&idade='.$idade.'&nocache=0.7636039437638835');

        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/xml, application/x-www-form-urlencoded;charset=ISO-8859-1, text/xml; charset=ISO-8859-1','Cookie: ASPSESSIONIDSCCRRTSA=NGOIJMMDEIMAPDACNIEDFBID; FGTServer=2A56DE837DA99704910F47A454B42D1A8CCF150E0874FDE491A399A5EF5657BC0CF03A1EEB1C685B4C118A83F971F6198A78','Host: HOST']);

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);

        $idade= GetStr($result, 'idade="', '"');


        echo "<script>$('#PASSOU').append('<font color=\"#3ae374\"> $idade  </font>' + '<br>');</script>";

    }
    
}
?>
  • Study the structure try/catch

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.