2014-03-07 71 views
1

我一直在嘗試gcm使用PHP,我已經在兩臺服務器上運行相同的代碼本地/現場。本地運行良好,但活着的人得到未經授權的錯誤 這裏是我的代碼。請幫我指出錯誤。 注意:我第一次運行時就開始運行了。 它駐留在主機上我主持我的網站gcm未授權的錯誤php

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

回答

0

cURLl目前not supported在App Engine上。

相當直接地將您的代碼轉換爲使用http streams,事實證明這對於應用引擎非常有效。

+0

對不起,添加錯誤的標籤,我沒有使用應用程序引擎 –