gcm未经授权的错误php

时间:2014-03-07 15:25:45

标签: php android google-cloud-messaging

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

`<?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设置

1 个答案:

答案 0 :(得分:0)

目前,App Engine上的cURLl为not supported

相当直接将代码转换为使用http streams,因为事实证明它在app引擎上非常有效。