i want to send a notification to (Segments) target like ["Active Users"] with parameter "included_segments" but When i call the API using CURL in PHP i get this Error : (Please include a case-sensitive header of Authorization: Basic or Bearer token="" with a valid REST API key.).
however the code is running well when i change target from parameter "included_segments" to "include_player_ids" .. but i want "included_segments" .. please HELP Me!.
- this is my Code :
`
function Copts($titlEN,$titlAR,$contEN,$contAR,$icon,$img){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://onesignal.com/api/v1/notifications',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"app_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"included_segments": ["Active Users"],
"contents": {"en": "'.$contEN.'"},
"headings": {"en": "'.$titlEN.'"},
"global_image": "'.$img.'",
"large_icon": "'.$icon.'"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
)
));
return json_decode(curl_exec($curl),true);
}
`
2
Answers
The problem was the REST API KEYS are not correct. USER AUTH KEY instead of REST API KEYS found it here :
See documentation here : https://documentation.onesignal.com/docs/accounts-and-keys
.
It might help if you showed the values to:
($titlEN,$titlAR,$contEN,$contAR,$icon,$img)
Add this to your
CURLOPT_HTTPHEADER
I looked at the example curl in the documentation
This is their example:
This is how their curl translates to PHP using https://curlconverter.com/php/:
I think all the
n
and extraneous spaces could be removed.I do not recommend using
Use this to make your HTTPHEADER:
I prefer the individual
curl_setopt()
rather than
curl_setopt_array()
but I’m old, almost 70 years old.
and my profile photo is current.