将通知发送给谷歌云端通讯用PHP给我未经授权错误401(Sending notifications

2019-06-27 20:28发布

搜索有关如何发送使用GCM通知一些信息,但与PHP而不使用servlet,我发现这一点: GCM与PHP(谷歌云通讯)

我测试了这些问题的答复的工作代码,也是我创造了浏览器的应用程序(与参照网址)一个重点,我给的权限到该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);
?>

Answer 1:

解决了!!!

您必须使用服务器应用程序键(与IP锁定),而不是浏览器的关键

:)



Answer 2:

你需要使用的API密钥的领域服务器应用的关键在你的服务器端的编码。 在创建服务器密钥不输入IP地址段内的任何东西。



文章来源: Sending notifications to Google Cloud Messaging with php gives me Unauthorized Error 401