-
-
Save rhapsodixx/11078447 to your computer and use it in GitHub Desktop.
Simple GCM Call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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