【问题标题】:gcm unauthorized error phpgcm未经授权的错误php
【发布时间】:2014-03-07 15:25:45
【问题描述】:

我一直在尝试使用 php 的 gcm,我在两台本地/实时服务器上运行了相同的代码。本地运行良好,但现场运行出现未经授权的错误 这是我的代码。请帮我指出错误。 注意:我第一次设置它时正在运行。 它驻留在我托管我的网站的主机上

`<?php
  //generic php function to send GCM push notification
   function sendPushNotificationToGCM($registatoin_ids, $message) {
    //Google cloud messaging GCM-API url
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );

    // Google Cloud Messaging GCM API Key
    define("GOOGLE_API_KEY", "MY KEY IN HERE");   
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );

        $ch = curl_init();
        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_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);      
        if ($result ==FALSE) {

            die('Curl failed: ' . curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }
?>
<?php
require './config.php'; 
  //this block is to post message to GCM on-click
$sql = new dbHandler();
$res = $sql->getgreg();
$ids = array();
foreach ($res as $key => $value) {
  $ids =$value['googlereg'];  
}

  $pushStatus = "";
  if(isset($_POST["message"])) {
    $gcmRegID  = $ids;
    $pushMessage = $_POST["message"];

    if (isset($gcmRegID) && isset($pushMessage)) {   
      $gcmRegIds = array($gcmRegID);
      $message = array("m" => $pushMessage);
      $pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);

    }  
  }

  //this block is to receive the GCM regId from external (mobile apps)
  if(!empty($_POST["shareRegId"])) {
    $gcmRegID  = $_POST["regId"];
   $res = $sql->addreg($gcmRegID);
   if($res ==1){
      echo 'Ok';
   }
 else {
      echo "error occoured!"; 
   }

  }
?>
<html>
    <head>
        <title>Push Test</title>
    </head>
  <body>
    <h1>Push Message</h1>
    <form method="post" action="index.php">                                     
      <div>                               
        <textarea rows="2" name="message" cols="23" placeholder="Message to transmit"></textarea>
      </div>

      <div><input type="submit"  value="Send Push Notification " /></div>
    </form>
    <p><h3><?php echo $pushStatus; ?></h3></p>       
    </body>
</html>`

是的,我已经设置了 IP

【问题讨论】:

    标签: php android google-cloud-messaging


    【解决方案1】:

    cURLl 目前在 App Engine 上为 not supported

    将您的代码转换为使用http streams 相当简单,事实证明这在应用引擎上非常有效。

    【讨论】:

    • 抱歉添加了错误的标签,我没有使用app-engine
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2023-03-18
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多