3 回答
TA贡献1820条经验 获得超10个赞
供将来参考,而不是无限循环垃圾邮件发送我使用过的请求sleep和break,因此它将每 10 秒尝试一次,最多 1 分钟,如下所示:
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);
}
TA贡献1820条经验 获得超9个赞
你可以使用类似的东西
$processCompleted = false;
$interval = 2; //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);
TA贡献1805条经验 获得超10个赞
do{
$response = curl_exec($request);
$httpcode = curl_getinfo($request, CURLINFO_HTTP_CODE);
}while($httpcode !== 200);
请注意,如果没有 sleep(),它会尽可能快地询问 url,“完成了吗?完成了吗?完成了吗?” 一遍又一遍,如果你想在答案不是 200 时睡觉,也许可以尝试
for(;;){
$response = curl_exec($request);
$httpcode = curl_getinfo($request, CURLINFO_HTTP_CODE);
if($httpcode===200){break;}
sleep(1);
}
- 3 回答
- 0 关注
- 227 浏览
添加回答
举报