gcm未授权错误php

gcm未授权错误php,php,android,google-cloud-messaging,Php,Android,Google Cloud Messaging,我一直在使用php尝试gcm,我在两台本地/实时服务器上运行了相同的代码。本地的运行良好,但活动的运行出现未经授权的错误 这是我的密码。请帮我指出错误。 注意:我第一次设置它时正在运行。 它位于我的网站所在的主机上 `<?php //generic php function to send GCM push notification function sendPushNotificationToGCM($registatoin_ids, $message) { //Goo

我一直在使用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

cURLl当前在App Engine上


非常直接地将您的代码转换为在app engine上非常有效的代码。

很抱歉添加了错误的标记,我没有使用app engine