gpt4 book ai didi

php - 如何在不使用 cURL 的情况下在 php 中发送 gcm 消息?

转载 作者:可可西里 更新时间:2023-11-01 00:45:10 24 4
gpt4 key购买 nike

我正在使用以下代码向 gcm 服务器发出 HTTP POST 请求。该代码始终返回“未经授权的错误 401”。我读到它与标题有关,但无法弄清楚出了什么问题。谁能告诉我怎么了?还有其他方法可以发送 gcm 消息吗? 任何帮助将不胜感激。

$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0";

$message = array("msg_url" => $msg_url, "msg_title" => $msg_title);

$url = 'https://android.googleapis.com/gcm/send';

$fields = array('registration_ids' => $ids,'data'=> array( "message" => $message ));

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

// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header'=> $headers ,
'method' => 'POST',
'content' => json_encode($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

最佳答案

使用我的 API key 测试了您的代码。我能够向 GCM 发送请求并得到正确的响应。您的 API key 似乎有问题

<?php

$msg_url = "http://msg.com";

$msg_title = "message_title";

$ids = array('reg_id_1', 'reg_id_2');

$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0";

$message = array("msg_url" => $msg_url, "msg_title" => $msg_title);

$url = 'https://android.googleapis.com/gcm/send';

$fields = array('registration_ids' => $ids,'data'=> array( "message" => $message ));

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

// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header'=> $headers ,
'method' => 'POST',
'content' => json_encode($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);
?>

关于php - 如何在不使用 cURL 的情况下在 php 中发送 gcm 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com