我正在编写一个PHP Curl Code来将数据从网站发送到Firebase。
但它没有保存在Firebase数据库中。
我为此编写了以下代码....
<?php
$message = 'msg_1';
$title = 'title_1';
$url = 'https://fcm.googleapis.com/fcm/send';
$server_key = 'SERVER_KEY';
$key = 'db_key';
$headers = array
(
'Authorization:key ='. $server_key,
'Content-Type: application/json'
);
$fields = array
(
'to' => $key,
'users'=>array
(
'username' => $title,
'message' => $message
)
);
$payload = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
它在线下回应我 {&#34; multicast_id&#34;:5950089806102679843,&#34;成功&#34;:0,&#34;失效&#34;:1,&#34; canonical_ids&#34;:0,&#34;结果& #34;:[{&#34;错误&#34;:&#34; InvalidRegistration&#34;}]}
请告诉我问题
答案 0 :(得分:0)
如果保存到firebase数据库,请写下此内容。
<?php
// Constants
$FIREBASE = "_YOUR_FIREBASE_URL_";
$NODE_SAVE = "users.json";
$message = 'msg_1';
$title = 'title_1';
$data = array(
'username' => $title,
'message' => $message
);
// JSON encoded
$json = json_encode( $data );
// Initialize cURL
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $FIREBASE . $NODE_SAVE );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "PUT" );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $json);
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
// Close connection
$response = curl_exec( $curl );
curl_close( $curl );
// Show result
echo $response . "\n";
?>
但你的代码。这意味着发送FCM。
是否要发送FCM或firebase数据库记录?