gcm unauthorized error php

0 views
Skip to first unread message

Kevin Kaburu via StackOverflow

unread,
Mar 7, 2014, 10:26:11 AM3/7/14
to google-appengin...@googlegroups.com

I have been trying out gcm using php, I have run the same code on two servers local/live one. The local one is running well but the live one is getting UNAUTHORIZED ERROR here is my code. please help me point out the error. NOTE: I was running the first time I set it up. It resides on the host on which I have hosted my site

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

And yes I have the ip set up



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/22254095/gcm-unauthorized-error-php

Stuart Langley via StackOverflow

unread,
Mar 7, 2014, 5:11:19 PM3/7/14
to google-appengin...@googlegroups.com

cURLl is currently not supported on App Engine.

Fairly straight forward to convert you code to use http streams which as it turns out would be very efficient on app engine.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/22254095/gcm-unauthorized-error-php/22261668#22261668
Reply all
Reply to author
Forward
0 new messages