How do I do a 1-second while Sleep from a json value? PHP

Asked

Viewed 39 times

-1

Eai personal am new with PHP

I did a CURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5112/resultado/?id='.$id.'');
curl_setopt($ch, CURLOPT_ENCODING, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, arraY('Content-Type: application/x-www-form-urlencoded','user-agent: Mozilla/5.0 (Linux; Android 5.1.1; G011A) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Mobile Safari/537.36'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_PROXY, 'p.webshare.io:80');
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'frbeibpp-rotate:j8ywaubloau6');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$resultadodaconta = curl_exec($ch);

that sends the request to the API and returns me in json the following:

[ { "760ea20deb62c7906edae38c6f0b60e0": [ { "status": [ "Rodando" ] } ] } ]

i want while the status is running I want you to do a while sleep of 1 second and when the status changes to completed I want it to display the results.

1 answer

0

Oops, for you to read a json in php is with the function json_decode, she will interpret the json and return you a object or array. To solve this example of yours, I will use the return of json as an array, because I found the json structure strange, since the key is a random string.

Usually, you expect something like:

{
   "token": "760ea20deb62c7906edae38c6f0b60e0"
}

But to solve this your problem goes in the same gambiarra, because the json is object inside array and tals.

$json = json_decode('[ { "760ea20deb62c7906edae38c6f0b60e0": [ { "status": [ "Rodando" ] } ] } ]', true); // pega o json e converte pra um array

$token = array_keys($json[0]); // pega a chave do seu json de forma dinâmica, já que é uma string aleatoria

$status = $json[0][$token[0]][0]['status'][0]; // pega o status de acordo com a chave que foi obtida na linha acima

if($status == 'Rodando') {
    // while
    // sleep
    // sua lógica aew
}

Browser other questions tagged

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