Php 为什么curl_exec()在我的web服务中返回响应?
为什么在我的web服务中返回数组 我创建了一个web服务,用Firebase发送推送通知。除了通知的结果随服务器响应而来,即使我没有问它,所有的事情都可以很好地工作Php 为什么curl_exec()在我的web服务中返回响应?,php,json,curl,Php,Json,Curl,为什么在我的web服务中返回数组 我创建了一个web服务,用Firebase发送推送通知。除了通知的结果随服务器响应而来,即使我没有问它,所有的事情都可以很好地工作 public function send_notification($recipient_id,$recipient_type,$request_status) { $url = "https://fcm.googleapis.com/fcm/send"; $token = $user_device_token;
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_通知函数:
$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时也会生成通知响应,并附加到响应中
第一行是我不想要的,也不是我期望的。curl\u exec的默认行为是将响应直接打印到stdout。如果只想将响应作为变量访问,则需要设置一个附加选项:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec的默认行为是直接将响应打印到stdout。如果只想将响应作为变量访问,则需要设置一个附加选项:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);