2017-08-01 82 views
0

我试图通过使用PHP的Firebase的fcm服务发送通知。以下是我走到这一步:Firebase FCM错误JSON_PARSING_ERROR:意外的令牌END 012 FI位于

$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; 

不过,我正从火力Unexpected token END OF FILE at position 2响应。

你认为这是怎么回事?

回答

3

删除内容长度线:

$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;