Google Cloud Messaging - GCM in Zend Framework PHP

June 29th 2012

Ok, exciting times! I can't believe that it's IO time again for Google. This year is amazing so far. I particularly like the new GCM (Google Cloud Messaging) service.

I thought I'd put some key instructions down on my blog here so that developers have somewhere to start from.

This is not by any means a finished piece of code, but using it seems to work. However, Googles servers are extremely busy and are currently 'unavailable' to process messages.

PHP Code:
<?php
$url 
'https://android.googleapis.com/gcm/send';
$serverApiKey "YOUR API KEY AS GENERATED IN API CONSOLE";
$reg "DEVICE REGISTRATION ID";

$data = array(
        
'registration_ids' => array($reg),
        
'data' => array('yourname' => 'Joe Bloggs')
);

$client = new Zend_Http_Client($url);
$client->setMethod('POST');
$client->setHeaders(array("Content-Type" => "application/json""Authorization" => "key=" $serverApiKey));
$client->setRawData(json_encode($data));
$request $client->request('POST');
$body $request->getBody();
$headers $request->getHeaders();
print(
"<xmp>");
var_dump($body);
var_dump($headers);

And there we have it. A working (it will work soon) example of using Googles new GCM in Zend Framework PHP

Have you got anything to add to this? If so, please let me know!

Jakob commented on Oct 19th 2012

Thanks so far for that Code. I like that you are using Zend_Http_Client what I will use now too instead of curl.

I prefere ' instead of " to mark strings when coding php.

why did you make the var_dump?