2

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

<?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;

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

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

代码:

<?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
4

2 回答 2

3

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

于 2013-01-15T11:52:38.163 回答
2

我遇到了同样的错误,因为我的数组索引没有正确排序,我使用 PHP 的sort()函数修复了它。

排序前

$registration_ids = Array
(
    [1] => "xxx"
    [6] => "xxx"
    [8] => "xxx"
)

种类

sort($registration_ids);

排序后

$registration_ids = Array
(
    [0] => "xxx"
    [1] => "xxx"
    [2] => "xxx"
)

传递给 PHP 的 cURL

$fields = array(
    'registration_ids' => $ids,
    'data' => array('message' => $data)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
于 2015-08-31T11:14:18.180 回答