我有一个通过 cURL 请求与 API 交互的 PHP 函数。问题是,一旦它被调用,它就会永远运行,一次又一次地请求相同的东西,而不是仅仅发出 1 个请求。它在短短几秒钟内发出了一千个相同的电话。
我对 curl 很陌生 - 你能告诉我我做错了什么吗?
function zapier_send_br($data){
// Create a new cURL resource
$ch = curl_init(ZAPIER_BR_URL);
// Set up request data to send json via POST
$fields = array(
'pp_br' => array(
'token' => 'xxx',
'data' => $data
)
);
$payload = json_encode($fields);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = json_decode(curl_exec($ch));
// Close cURL resource
curl_close($ch);
return $result;
}
我怎样才能让它只运行一次(或直到一个成功的请求)?
函数在 foreach 循环中运行,但循环本身只运行了 1-2 次,并对其进行了测试和记录。
我在同一页面上也有其他 cURL 请求(都以相同的方式运行)-我是否应该使用 curl_reset($ch) 来确保它们不会相互干扰?