doukong1391 2012-08-05 06:15
浏览 52

使用Zend框架从PHP服务器收到HttpResponse 400

I am trying to send push notifications to my android application using PHP server with Zend framework, but i am getting the HttpResponse as : The request could not be parsed as JSON or contains invalid fields, with the error code 400. I have used the server code as given in the demo of Zend framework, which is as follows :

<?php
require_once 'Zend/Mobile/Push/Gcm.php';
require_once 'Zend/Mobile/Push/Message/Gcm.php';

$regId = $_POST["registrationid"];

$message = new Zend_Mobile_Push_Message_Gcm();
$message->setId(time());
$message->addToken($regId);
$message->setData(array(
    'foo' => 'bar',
    'bar' => 'foo',
));

$gcm = new Zend_Mobile_Push_Gcm();
$gcm->setApiKey('my api key');

try {
    $response = $gcm->send($message);
} catch (Zend_Mobile_Push_Exception $e) {
    // all other exceptions only require action to be sent or implementation of exponential backoff.
    die($e->getMessage());
}

// handle all errors and registration_id's
foreach ($response->getResults() as $k => $v) {
    if ($v['registration_id']) {
        printf("%s has a new registration id of: %s
", $k, $v['registration_id']);
    }
    if ($v['error']) {
        printf("%s had an error of: %s
", $k, $v['error']);
    }
    if ($v['message_id']) {
        printf("%s was successfully sent the message, message id is: %s", $k, $v['message_id']);
    }
}
  • 写回答

1条回答 默认 最新

  • doubo9799 2012-08-05 22:03
    关注

    I don't have my exact URL handy and I'm trying on my phone, but I touched on this recently in my blog. See http://www.rogerethomas.com What you will need is to use a browser API key, not a server one. Don't ask why but it seems there's an error with Googles documentation.

    Edit: here's a recent link to my answer from another similar question: GCM with PHP (Google Cloud Messaging)

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?