Last active
August 29, 2015 14:19
-
-
Save generaleye/d41efaab50ccf688315f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
//USAGE | |
$gcm = new GoogleGCMApi($device, "" . json_encode(array('type' => "approve", 'message' => "Your Request has been Approved")) . ""); | |
$gcm->send(); | |
?> | |
<?php | |
/** | |
* Google Cloud Messaging API Class | |
* Author: Generaleye | |
*/ | |
class GoogleGCMApi { | |
private $gcm_url = 'https://android.googleapis.com/gcm/send'; | |
private $device_id; | |
private $message; | |
function __construct($device_id,$message) { | |
$this->device_id = $device_id; | |
$this->message = $message; | |
} | |
public function send() { | |
$fields = array( | |
'registration_ids' => array( $this->device_id ), | |
'data' => array( "message" => $this->message ), | |
); | |
$headers = array( | |
'Authorization: key=' . GOOGLE_GCM_APIKEY, | |
'Content-Type: application/json' | |
); | |
// Open connection | |
$ch = curl_init(); | |
// Set the url, number of POST vars, POST data | |
curl_setopt( $ch, CURLOPT_URL, $this->gcm_url ); | |
curl_setopt( $ch, CURLOPT_POST, true ); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) ); | |
// Execute post | |
$result = curl_exec($ch); | |
if ( curl_errno( $ch ) ){ | |
echo 'GCM error: ' . curl_error( $ch ); | |
} | |
// Close connection | |
curl_close($ch); | |
echo $result; | |
} | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment