gpt4 book ai didi

php - Android : GCM using PHP 错误

转载 作者:太空狗 更新时间:2023-10-29 16:43:16 25 4
gpt4 key购买 nike

我在 PHP 中使用以下代码向 Android 发送 GCM 消息:

<?php
$apiKey = "xxxxx"; //my api key


$registrationIDs = array("APA91bGGN7o7AwVNnv35lwP5Jw8OTJQL331XcxPfEIu4xt-ZKLe6R0aSSbAve99uKSDXhzE9L2PVLihpqFt0DEawhymUs9h5ICbTMweMAEJypg6ZLFqmf6SOGlyULQzudw9MM1DjbPaaKbo--wxWoHGkjyec2H_63e7mesYjaRf4_rgxBe655M0");



// Message to be sent
$message = "Message Text";

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

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

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

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
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, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);

echo $result;

它工作正常。在这里,我手动(硬编码)添加了我的设备 token 。

但是当我尝试从数据库中获取设备 token 时,我遇到了下面提到的错误。

代码:

<?php
include 'config.php';
if($_REQUEST['msg']!='')
{
echo $message = $_REQUEST['msg'];
// Replace with real BROWSER API key from Google APIs
$apiKey ="xxxxxx"; //my api key
// Replace with real client registration IDs
$sql = mysql_query("SELECT `device_token` FROM `users`");
$result = mysql_fetch_array($sql);


$registrationIDs = $result;

// Message to be sent
$message = $_REQUEST['msg'];

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

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

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

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
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, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);

echo $result;

}

我也尝试过使用 json_encode($registration_ids) 但没有用。

"registration_ids" field is not a JSON array

最佳答案

我终于找到了解决办法。我使用 for 循环将所有结果放入一个数组,并使用 json_encode();

将数组转换为 JSON 数组

关于php - Android : GCM using PHP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14334921/

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