php - 将 MySQL 结果集转换为 JSON 数组以使用 Google GCM API 时出现的问题

标签 php mysql google-cloud-messaging

我正在开发一个应用程序,我必须在其中集成 GCM 功能。 收件人的 device_id 存储在 MySQL 数据库中,然后通过 CURL 检索这些数据库并将其发送到 Google GCM API url。 问题是我这样做时收到错误:

 Recipients_Id field is not a JSON Array.

PHP代码是:

      //get the recipients gcm_ids
     if (isset($_POST["deviceid"]) && isset($_POST["message"]) && isset($_POST["recipient_id"])) {
      require_once __DIR__ . '/db_connect.php';
include_once './GCM.php';
$gcm = new GCM();
 // connecting to db
$db = new DB_CONNECT();
 $device_id=$_POST['deviceid'];
$recipient_id=$_POST['recipient_id'];
$get_details=mysql_query("select id,name from gcm_users where gcm_regid='$device_id' ");
$row_details=mysql_fetch_array($get_details);
$user_name=$row_details["name"];
 $user_id=$row_details["id"];
$recipients_gcm_ids=mysql_query("SELECT  gcm_regid from gcm_users where id='$recipient_id'");
$rec_gcm1=array();
 while($row = mysql_fetch_assoc($recipients_gcm_ids))
 {array_push($rec_gcm1,$row);}
$recipients_gcm=array();
$recipients_gcm=json_encode($rec_gcm1);
$message = array("price" => $message1);
$result = $gcm->send_notification($recipients_gcm, $message);


//The class that sends the message to the Google API url
         class GCM {

//put your code here
// constructor
function __construct() {

}

/**
 * Sending Push Notification
 */
public function send_notification($Recipients_Id, $message) {
    // include config
    include_once './config.php';

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

    $fields = array(
        'Recipients_Id' => $Recipients_Id,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        '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);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    echo "Result Is:".$result;
}

我知道问题涉及这行代码:

 $result = $gcm->send_notification($recipients_gcm, $message);

但是,除了使用“json_encode”函数之外,我还不知道将使用任何方法在 PHP 中创建 JSON 数组。也许还有另一种方法可以规避:

       "Not a JSON Array" error  ?

顺便说一下 print_r($recipients_gcm) 的输出是:

    [{"gcm_regid": "ABCDEFGH12y63i4455u65y4i4p4yu4t3i3zzttyuuiioo"}]

最佳答案

我在代码中发现了几个问题:

您正在将 reg id 的 json 字符串发送到您的 send_notification 函数。相反,您应该发送一组 reg id。尝试取出这两行并使用数组 $rec_gcm1 代替。

$recipients_gcm=array();
$recipients_gcm=json_encode($rec_gcm1);

改为调用此:

$result = $gcm->send_notification($rec_gcm1, $message);

接下来,在 send_notification 函数中,将 $fields 更改为:

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

即将您的 Recipients_Id 键更改为 registration_ids,否则 Google GCM 服务器将无法解析它。

关于php - 将 MySQL 结果集转换为 JSON 数组以使用 Google GCM API 时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15432397/

相关文章:

php - 如何将 Symfony HttpClient 模拟为依赖项?

php - 如何计算belongsTo模型laravel的总和

MySQL SELECT From TABLE WHERE FIELD TITLE 不喜欢

javascript - 在 Ajax 中测试 PHP 响应

PHPUnit:测试对象数组

mysql - MySQL 中的错误 "Every derived table must have its own alias"是什么?

php - 如何使用 JOIN 管理来自 3 个不同表的 SELECT 数据?

android - 无法导入 import com.google.android.gcm.GCMBaseIntentService

android - 如何在通知中心设置 GCM/FCM 负载的生存时间

android - 如何处理 GCM 更改注册 ID?