Created
May 9, 2013 14:59
-
-
Save mikamboo/5548002 to your computer and use it in GitHub Desktop.
Using PHP cURL to send a message (with retry in case of failure) to a Google Cloud Messaging registered device.
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 | |
define('CURL_EXEC_RETRY_MAX',3); | |
define('GCM_SERVER_URL',"https://android.googleapis.com/gcm/send"); | |
define('GCM_API_KEY',"YOUR_API_KEY"); | |
/** | |
* Using PHP to send a message to a Google Cloud Messaging registered device is quite simple. | |
* In this class we're using curl to create a message request to send to the Google GCM server. | |
* This class require an GCM Api key cf. {@link https://code.google.com/apis/console/} | |
* | |
* @author Michael | |
* | |
*/ | |
class App_GCMdriver { | |
private $ch; | |
private $response; | |
private $info; | |
private $http_code; | |
public function __construct() { | |
//-- init cURL an set options | |
$this->ch = curl_init(); | |
curl_setopt( $this->ch, CURLOPT_URL, GCM_SERVER_URL ); | |
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, 0 ); | |
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, 0 ); | |
curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, true ); | |
} | |
/** | |
* The following function will send a GCM notification using curl. | |
* | |
* @param $apiKey [string] The Browser API key string for your GCM account | |
* @param $registrationIdsArray [array] An array of registration ids to send this notification to | |
* @param $messageData [array] An named array of data to send as the notification payload | |
*/ | |
public function sendNotification( $registrationIdsArray, $messageData ) | |
{ | |
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . GCM_API_KEY); | |
$data = array( | |
'data' => $messageData, | |
'registration_ids' => $registrationIdsArray | |
); | |
//-- Complementary options for GCM service | |
curl_setopt( $this->ch, CURLOPT_HTTPHEADER, $headers ); | |
curl_setopt( $this->ch, CURLOPT_POSTFIELDS, json_encode($data) ); | |
return $this->exec(); | |
} | |
/** | |
* Execute query with retry | |
*/ | |
private function exec(){ | |
$this->retryExec(); | |
$this->info = curl_getinfo($this->ch); | |
$this->http_code = $this->info['http_code']; | |
$response = $this->response; | |
curl_close($this->ch); | |
return $response; | |
} | |
/** | |
* Query with retry executor | |
*/ | |
private function retryExec(){ | |
$i = 0; | |
while ($i++ < CURL_EXEC_RETRY_MAX) { | |
$this->response = curl_exec($this->ch); | |
if ($this->response) break; | |
if ($i < CURL_EXEC_RETRY_MAX) sleep($i); | |
} | |
} | |
public function getHttpCode() {return $this->http_code;} | |
public function getExecResponse() {return $this->response;} | |
public function getExecInfo() {return $this->info;} | |
public function getError() {return curl_error($this->ch);} | |
} | |
$regId = "A_DEVICE_REGISTRATION_ID"; | |
$gcm = new App_GCMdriver(); | |
$result = $gcm->sendNotification( array($regId), array('message' => 'hello !!') ); | |
print_r($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment