Onesignal REST API 来自 php 的请求请包含 case-sensitive header 授权:基本

Onesignal REST API Request from php Please include a case-sensitive header of Authorization: Basic

我正在尝试使用 Onesignal rest 发送通知 API,我已经在我的 onesignal 帐户中设置了一个新应用程序。我已经确定 REST API 键和 app_id 是正确的,但我仍然收到以下错误

 {"errors":["Please include a case-sensitive header of Authorization: Basic \u003cYOUR-REST-API-KEY-HERE\u003e or Bearer token=\"\u003cYOUR-REST-API-KEY-HERE\u003e\" with a valid REST API key."],"reference":["https://documentation.onesignal.com/docs/accounts-and-keys#section-keys-ids"]}

我在一个信号帐户中只有一个应用程序 我用 POSTMAN 测试了其余的 api 并将请求类型更改为 POST 就像一个魅力 但是使用 PHP 得到了错误。

这是发送请求的示例代码

$ch = curl_init();
$header = array();
$header[] = "Content-type: application/json;";
$header[] = "authorization: Basic <MY-REST-API-KEY>";
$fields = array(
          'app_id' => '<MY-APP-ID>',
          'filters' => array(array("field" => "tag", "key" => "player_id", "relation" => "=", "value" => "48223a70-6fb0-11ec-a059-d208b7d4ecd7")),
                    'contents' => array("en"=> "Test From front end"),
      );
print_r($header);
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$res = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($res);

我终于设法使用以下代码解决了问题

我想问题是我缺少一些参数,例如

  1. CURLOPT_CUSTOMREQUEST
  2. CURLOPT_HTTP_VERSION
  3. CURLOPT_MAXREDIRS
$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": "<My-App-ID",
          "data": {"foo": "bar"},
          "include_player_ids":["<player_id>"],
          "contents": {"en": "'.$message.'"},
          "headings":{"en":"'.$header.'"}
        }',

          CURLOPT_HTTPHEADER => array(
            'Authorization: Basic <My-REST_API-Key',
            'Content-Type: application/json'
          ),
        ));

        $response = curl_exec($curl);