I want to convert my array to a Json String and use it for send message using Firebase Cloud Messaging Server.
My array is :
$data = array(
"to" => "cMbucIFcJMA:APA91bH_Wq0sPrMhTmhJ9gPJX7GUsRo...",
"notification" =>
array(
"body" => "sadeq",
"title" => "test notification"
)
);
and when I use json_encode($data)
my back String is:{"to":"cMbucIFcJMA:APA91bH_Wq0sPr.... ","notification":"Array"}
how can I convert this array to valid Json without have Array
in my notification String???
My completely function is:
public function SendFromPost(){
$data = array(
"to" => "cMbucIFcJMA:APA91bH_..... ",
"notification" =>
array(
"body" => "sadeq",
"title" => "test notification"
)
);
$ch = curl_init();
curl_setopt($ch , CURLOPT_URL , "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch , CURLOPT_POST , 1);
curl_setopt($ch , CURLOPT_POSTFIELDS , $data );
curl_setopt($ch , CURLOPT_RETURNTRANSFER , true);
$headers = [
'Content-Type: application/json
',
'Authorization: key=AIzaS.......'
];
curl_setopt($ch , CURLOPT_HTTPHEADER , $headers);
$server_output = curl_exec($ch);
curl_close($ch);
return var_dump(json_encode($data));
}
My code is correct. but I don't know why my php do like this.
Your code works. You've got an extra ;
that isn't valid after your nested array.
<?php
$data = array(
"to" => "cMbucIFcJMA:APA91bH_Wq0sPrMhTmhJ9gPJX7GUsRo...",
"notification" =>
array(
"body" => "sadeq",
"title" => "test notification"
)
);
var_dump(json_encode($data));
Yields
string(115) "{"to":"cMbucIFcJMA:APA91bH_Wq0sPrMhTmhJ9gPJX7GUsRo...","notification":{"body":"sadeq","title":"test notification"}}"