Firebase FCM错误JSON_PARSING_ERROR:位置

时间:2017-08-01 08:40:37

标签: php firebase firebase-cloud-messaging

我正在尝试使用php通过firebase的fcm服务发送通知。这是我到目前为止所得到的:

$ch      = curl_init();
$payload = [
    'to'   => '/topics/'.ANDROID_TOPIC,
    'notification' => [
        'message' => 1
    ]
];
$headers = [
    'Content-Type: application/json',
    'Content-length: '.sizeof(json_encode($payload)),
    'Authorization: key='.FIREBASE_KEY
];

curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));

$result = curl_exec($ch );
curl_close($ch);

return $result;

但是,我收到来自firebase的Unexpected token END OF FILE at position 2回复。

您认为发生这种情况的原因是什么?

1 个答案:

答案 0 :(得分:4)

删除内容长度行:

$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: key=' . FIREBASE_KEY;

$payload = [
  'to' => 'verybigpushtoken',
  'notification' => [
    'title' => "Portugal VS Germany",
    'body' => "1 to 2"
  ]
];

$crl = curl_init();
curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POST,true);
    curl_setopt($crl, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($crl, CURLOPT_POSTFIELDS, json_encode( $payload ) );

curl_setopt($crl, CURLOPT_RETURNTRANSFER, true );
// curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, false); should be off on production
// curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false); shoule be off on production

$rest = curl_exec($crl);
if ($rest === false) {
    return curl_error($crl)
}
curl_close($crl);
return $rest;