谷歌云消息返回未经授权使用php

时间:2013-04-02 18:49:54

标签: php google-cloud-messaging

我正在尝试使用php

向gcm发送请求

我发现这篇文章 GCM with PHP (Google Cloud Messaging)

我的代码:

 public function gcmSend($registrationIdsArray, $messageData) {
    // Replace with real BROWSER API key from Google APIs
    $apiKey = "my key";

    // Replace with real client registration IDs 
    $registrationIDs = $registrationIdsArray;

    // Message to be sent
    $message = $messageData;

    // 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));

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

    // Close connection
    curl_close($ch);

    return $result;
}

它总是回归:

<HTML>
    <HEAD>
        <TITLE>Unauthorized</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
        <H1>Unauthorized</H1>
        <H2>Error 401</H2>
    </BODY>
</HTML>

对于api密钥我尝试服务器密钥和浏览器密钥

为服务器密钥设置结果     $ _SERVER [ 'SERVER_ADDR'];

如果我的请求是http://www.api.mysite.com/test,则设置浏览器密钥

*.mysite.com/*

密钥可能有激活时间吗?

我的服务器是一个共享服务器,ip可以被列入黑名单?

由于

0 个答案:

没有答案