Skip to content

Instantly share code, notes, and snippets.

@Chiggins
Created April 1, 2014 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chiggins/9923471 to your computer and use it in GitHub Desktop.
Save Chiggins/9923471 to your computer and use it in GitHub Desktop.
<?php
// You put your own App ID and Sender IDs
define("GOOGLE_APP_ID", "AIxxxxxxxxx");
define("SENDER_ID", "xxxxxxxxxx");
// Device ID to send the message to
$deviceId = "insertdeviceidhere";
$message = "Message from PHP!";
// Create the POST request
$request = "https://android.googleapis.com/gcm/send";
$postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message={$message}&data.time=" . date("Y-m-d H:i:s") . "&registration_id={$deviceId}";
echo "Sending request to {$request} with postData {$postData}";
$session = curl_init($request);
// Set HTTP headers
curl_setopt($session, CURLOPT_HTTPHEADER, array("Authorization: key=" . GOOGLE_APP_ID, "Sender: id=" . SENDER_ID, "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"));
// Make it so it's a POST request
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $postData);
// Don't return headers, but get the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
echo "Sending...";
$response = curl_exec($session);
curl_close($session);
echo "Response: {$response}";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment