firebase messaging api php 订阅主题 411 错误

firebase messaging api php subscribe to topic 411 error

我正在尝试在我的网络应用程序中实施通知。我有这个 php 文件,我在其中发送通知:

<?php
  function sendGCM($title,$message, $id) {
    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = array (
        'registration_ids' => array (
                $id
        ),
        "notification" => array(
            "title" => $title,
            "body" => $message,
            "click_action" => "https://google.com"
        )
    );
    $fields = json_encode ( $fields );

    $headers = array (
        'Authorization: key=' . $MY_KEY,
        'Content-Type: application/json'
    );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    echo $result;
    curl_close ( $ch );
  }
?>

现在我想做的是向主题而不是单个 ID 发送通知。在 this page 上,它展示了如何将一个 ID 订阅到一个主题。这是我为此创建的函数:

function createTopic($topic,$id) {
  $url = 'https://iid.googleapis.com/iid/v1/' . $id . '/rel/topics/' . $topic;
  $headers = array (
    'Authorization: key=MY_KEY'
  );
  $ch = curl_init ();
  curl_setopt ( $ch, CURLOPT_URL, $url );
  curl_setopt ( $ch, CURLOPT_POST, true );
  curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
  curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );

  $result = curl_exec ( $ch );
  echo $result;
  curl_close ( $ch );
}

我从 Google 收到这个错误,我不知道出了什么问题:

  1. That’s an error.

POST requests require a Content-length header. That’s all we know.

有什么帮助吗?

example in the documentation 显示使用 Content-Length header:

https://iid.googleapis.com/iid/v1/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA/rel/topics/movies
Content-Type:application/json
Content-Length: 0
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

将其添加到您的请求中:

$headers = array (
    'Authorization: key=' . $MY_KEY,
    'Content-Length: 0'
);