Why does the OneSignal API throw me this error?

0

Create a notification application with the OneSignal API, and a moment ago everything worked excellent for me. But suddenly I get this error:

{
"response": {
       "status": 400,
       "error": "There was a problem in the JSON you submitted: unexpected characters after the JSON document at line 1, column 1 [parse.c:590]"
}
}

This is my php code:

<?php
header('Content-Type: application/json');
function sendMessage(){
$content = array(
"en" => 'Hi'
);
$fields = [
'app_id' => "xxxxxx-xxxx-xxxx-xxx-xxxxxxx",
'include_player_ids' => [
"xxxxxx-xxxx-xxx-xxxxxxx-xxxx"
],
'contents' => $content
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://onesignal.com/api/v1/notifications",
CURLOPT_HTTPHEADER => array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic xxxxxxxxxxxxxxxxxxxxx'),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_SSL_VERIFYPEER => FALSE
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
$response = sendMessage();
$return["response"] = $response;
$return = json_encode($return, JSON_PRETTY_PRINT);
echo $return;
?>

manifest.json :

{
      "name": "Page Name",
      "short_name": "Short Page Name",
      "start_url": "/",
      "display": "standalone",
      "gcm_sender_id": "xxxxxxx"
} 

Here is the documentation from the OneSignal API.

    
asked by Eddy Otsutsuki 17.03.2017 в 09:24
source

1 answer

0

In the end I solved it myself, the problem was that I was not using the function json_encode in variable $fields .

And my code stayed like this:

<?php
header('Content-Type: application/json');
function sendMessage(){
$fields = [
'app_id' => "xxxxxx-xxxx-xxxx-xxx-xxxxxxx",
'include_player_ids' => [
"xxxxxx-xxxx-xxx-xxxxxxx-xxxx"
],
'contents' => [
'en' => 'Hi'
]
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "https://onesignal.com/api/v1/notifications",
CURLOPT_HTTPHEADER => [
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxx'
],
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => json_encode($fields),
CURLOPT_SSL_VERIFYPEER => FALSE
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
$response = sendMessage();
$return["response"] = $response;
$return = json_encode($return, JSON_PRETTY_PRINT);
echo $return;
?>
    
answered by 18.03.2017 / 05:24
source