为什么curl_exec()在我的网络服务中返回一个数组?
我创建了一个Web服务,用于通过Firebase发送推送通知。 Everthing工作得非常好,除非通知的结果随服务器响应而来,即使我没有问过它。
public function send_notification($recipient_id,$recipient_type,$request_status)
{
$url = "https://fcm.googleapis.com/fcm/send";
$token = $user_device_token;
$notification = array(
'title' => $title,
'text' => $body,
'sound' => 'default',
'badge' => '1'
);
$arrayToSend = array(
'to' => $token,
'notification' => $notification,
'priority' => 'high'
);
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key=' . $server_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//Send the request
error_reporting(E_ERROR);
$response_notification = curl_exec($ch);
if ($response_notification === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $response_notification;
}
以下是我调用send_notification()函数的方法:
$this->send_notification($request->provider_id,'provider',0);
然后我为webapp的客户端部分打印成功消息:
$response_array = array(
'success' => true,
'request_id' => $requests->id,
'current_provider' => $request->provider_id,
'address' => $requests->s_address,
'latitude' => $requests->s_latitude,
'longitude' => $requests->s_longitude,
);
$response = response()->json($response_array, 200);
return $response;
即使我没有询问,也会在调用附加到响应的curl_exec($ ch)时生成通知响应。
第一行是我不想要的,我不期待。
答案 0 :(得分:4)
curl_exec
的默认行为是将响应直接打印到stdout。如果您只想将响应作为变量访问,则需要设置其他选项:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);