gpt4 book ai didi

php - 来自 while 循环的带有 cURL 的 json 发布

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:49:58 27 4
gpt4 key购买 nike

我正在尝试使用 cURL 发布 json 数据。这个想法是:如果 url 不可访问(例如互联网连接失败),请在成功时继续尝试发布 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;

}

最佳答案

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

关于php - 来自 while 循环的带有 cURL 的 json 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30096107/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com