Issue
I have a simple curl request in PHP like so:
$request = curl_init('https://some/path/');
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:'.$token));
$response = curl_exec($request);
$errors = curl_error($request);
$httpcode = curl_getinfo($request, CURLINFO_HTTP_CODE);
curl_close($request);
The url I'm calling will return a 202 if it is still preparing the data and a 200 when it is ready.
How can I build in logic to repeat the request until I receive a 200 response.
Solution
For future reference, rather than an infinite loop spamming the request I've used sleep
and break
, so it will try every 10 seconds for max 1 minute, like so:
for($i=0; $i<6; $i++){
$request = curl_init('https://some/path/');
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:'.$token));
$response = curl_exec($request);
$errors = curl_error($request);
$httpcode = curl_getinfo($request, CURLINFO_HTTP_CODE);
curl_close($request);
if($httpcode == 200){
var_dump($response);
break;
}
sleep(10);
}
Answered By - Paddy Hallihan