1

I'm trying to send a request to gcm using php

I find this post GCM with PHP (Google Cloud Messaging)

my code:

 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;
}

And it always return :

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

for the api key I try both server key and browser key

for the server key I set the result of $_SERVER['SERVER_ADDR'];

and for the browser key if my request is http://www.api.mysite.com/test I set

*.mysite.com/*

There is maybe an activation time for the key ?

my server is a mutualised server the ip can be blacklisted ?

Thanks

Community
  • 1
  • 1
Ajouve
  • 8,589
  • 22
  • 77
  • 123

0 Answers0