我想发送一个通知给(段)目标,比如参数"included_segments“的"Active User”,但是当我使用PHP中的CURL调用API时,我会得到这样的错误:(请包含一个区分大小写的授权头: Basic或Bearer token="“,其中包含一个有效的REST键。)
但是,当我将目标从参数"included_segments“更改为"include_player_ids”时,代码运行良好。但我想要"included_segments“。请帮帮我!
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);
}`
发布于 2022-11-19 16:31:47
问题是REST键是不正确的。用户AUTH KEY而不是REST键在这里找到了它:
仪表板->设置->键和ids
参见这里的文档:https://documentation.onesignal.com/docs/accounts-and-keys
。
发布于 2022-11-19 05:44:56
如果您将值显示给:($titlEN,$titlAR,$contEN,$contAR,$icon,$img),可能会有所帮助。
将此添加到CURLOPT_HTTPHEADER中
'Accept: application/json'我看了文档中的例子
这就是他们的例子:
curl --request POST \
--url https://onesignal.com/api/v1/notifications \
--header 'Authorization: Basic YOUR_REST_API_KEY' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"app_id": "string",
"included_segments": [
"string"
],
"external_id": "string",
"contents": {
"en": "English or Any Language Message",
"es": "Spanish Message"
},
"name": "INTERNAL_CAMPAIGN_NAME",
"send_after": "string",
"delayed_option": "string",
"delivery_time_of_day": "string",
"throttle_rate_per_minute": 0
}'这就是他们使用https://curlconverter.com/php/将curl转换为PHP的方式。
我认为所有的\n和多余的空间都可以移除。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization' => 'Basic YOUR_REST_API_KEY',
'accept' => 'application/json',
'content-type' => 'application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, '\n{\n "app_id": "string",\n "included_segments": [\n "string"\n ],\n "external_id": "string",\n "contents": {\n "en": "English or Any Language Message",\n "es": "Spanish Message"\n },\n "name": "INTERNAL_CAMPAIGN_NAME",\n "send_after": "string",\n "delayed_option": "string",\n "delivery_time_of_day": "string",\n "throttle_rate_per_minute": 0\n}');我不建议使用
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization' => 'Basic YOUR_REST_API_KEY',
'accept' => 'application/json',
'content-type' => 'application/json',
]);使用这个可以使HTTPHEADER:
$headers = array();
$headers[] = 'Authorization: Basic YOUR_REST_API_KEY';
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);我更喜欢个人的curl_setopt()
而不是curl_setopt_array()
但我老了,快70岁了。
我的个人资料照片是最新的。
https://stackoverflow.com/questions/74496251
复制相似问题