2

尋找關於如何發送使用GCM通知,但與PHP而不使用servlet的一些信息,我發現這一點:GCM with PHP (Google Cloud Messaging)將通知發送給谷歌雲端通訊用PHP給我未經授權錯誤401

我測試的工作代碼對這些問題的迴應,我還創造了瀏覽器的應用程序(與參照網址)一個重點,我給的權限到該IP:.mywebsite.com/(TE PHP文件是在這個網址:「http://www.mywebsite.com/~jma/cHtml5/cap/kk.php」)

但我得到這個迴應:未經授權的錯誤401

我做錯了什麼?

這是PHP文件:

<?php 
    // Replace with real server API key from Google APIs   
    $apiKey = "fictional key"; 

    // Replace with real client registration IDs 
    $registrationIDs = array("APA91asdasdSDGGS232S13S4213abGqiNhCIXKjlxrkUYe_xTgTacNGB5n16b380XDd8i_9HpKGRHkvm8DDet4_WK3zumjDEKkTRWLgPS7kO-BrKzWz7eWFQaDD9PJ8zA6hlSqL9_zH21P8K22ktGKmo_VIF6YAdU9ejJovrKBTpgQktYkBZBf9Zw","APAasdasd32423dADFG91bHYYxYB7bFiX5ltbJt6A-4MBiNg7l4RS4Bqf3jIfYviaaUfZ810XJo2o66DY9-jdeJk_JR8FIZCyrmCv-eu_WLkGZ8KaoHgEDR_16H2QPm98uHpe1MjKVXbzYc4J89WMmcIrl5tHhWQnIQNzaI6Zp6yyFUNUQ"); 

    // Message to be sent 
    $message = "Test Notificación PHP"; 

    // Set POST variables 
    $url = 'https://android.googleapis.com/gcm/send'; 

    $fields = array(
     'registration_ids' => $registrationIDs, 
     'data' => array("message" => $message), 
    ); 

    $headers = array(
     'Authorization: key=' . $apiKey, 
     'Content-Type: application/json' 
    ); 

    // Open connection 
    $ch = curl_init(); 

    // Set the url, number of POST vars, POST data 
    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_POSTFIELDS, json_encode($fields)); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    //curl_setopt($ch, CURLOPT_POST, true); 
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    // Execute post 
    $result = curl_exec($ch); 

    // Close connection 
    curl_close($ch); 
    echo $result; 
    //print_r($result); 
    //var_dump($result); 
?> 
+0

如果這是您的真實註冊ID,我建議您生成一個新的。 :) – kishu27

回答

10

解決!

您必須使用服務器應用程序鍵(與IP鎖定),而不是瀏覽器的關鍵

:)

+0

這是什麼一回事,IP鎖定,服務器應用程序? – Notepad

+1

@Fewtron _Server_和_Browser_特定鍵的概念允許您監視您的流量正在進行的位置,並允許在特定位置使用特定的鍵。 – jackyalcine

0

您需要使用服務器應用程序的關鍵在API密鑰的領域在你的服務器端編碼。 創建服務器密鑰時,請勿在IP地址字段內輸入任何內容。

相關問題