json发布了一个while循环的cURL

Mar*_*n k 5 php json curl

我试图用cURL发布json数据.这个想法是:如果网址无法访问(例如互联网连接失败),请继续尝试在成功时发布json.它工作,但当我把它放入while循环时它只执行一次.我究竟做错了什么:

$jsonDataEncoded = json_encode($event);
echo $jsonDataEncoded;
echo "\n";

$send_failure = true;

while ($send_failure) {

$url = "a";// intentionally inaccessible url

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:    application/json','Content-Length: ' . strlen($jsonDataEncoded))); 
$result = curl_exec($ch);
if(curl_errno($ch)){
    throw new Exception(curl_error($ch));

} else {$send_failure = false;}
return $result;

}
Run Code Online (Sandbox Code Playgroud)

小智 0

您必须使用 var_dump($result) 打印并检查 $result 中的值;。如果有任何连接错误,它会返回错误代码,您可以在 IF 条件下查看手册。希望这可以帮助你。