Skip to content

Instantly share code, notes, and snippets.

@rhapsodixx
Last active August 29, 2015 14:00
Show Gist options
  • Save rhapsodixx/11078447 to your computer and use it in GitHub Desktop.
Save rhapsodixx/11078447 to your computer and use it in GitHub Desktop.
Simple GCM Call
<?php
/**
* panji
* 2013 08 08
*/
class Gcm
{
protected static $apiUrl = "https://android.googleapis.com/gcm/send";
// browser key
protected static $apiKey = "AIikeyhere";
public static function send( $message = '', $registrationIDs = array() )
{
$result = "temp";
// if there is no registrationIDs or no message defined don't proceed
if( (count($registrationIDs) == 0) || ($message == null) )
{
return -1;
}
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message )
);
$headers = array(
'Authorization: key=' . self::$apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, self::$apiUrl );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// get status code
$http_status_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment