以下PHP程式碼使用plain text格式送出GCM請求封包。
define("GOOGLE_API_KEY",
"your_api_key");
$reg_id = 'device_reg_id';
//payload data
$payload = 'data.contentTitle='.urlencode('耕雲智慧').
'&data.contentText='.urlencode('測試訊息推播');
'&data.contentText='.urlencode('測試訊息推播');
//http header
$headers = array('Authorization: key=' . GOOGLE_API_KEY,
'Content-Type:application/x-www-form-urlencoded;charset=UTF-8');
'Content-Type:application/x-www-form-urlencoded;charset=UTF-8');
$fields = $payload.'®istration_id='.$reg_id;
//curl connection
$ch = curl_init();
curl_setopt($ch,
CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
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, $fields);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>