I am fairly new to the php syntax, I understand how to receive http post request, and also GET request in php, but I do not know how to correctly send out a http post request in php without a form, using the data below. (I understand I can use ajax, but I want to learn how to do it using a php script).
https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=I_PUT_THIS_AS_A_SAMPLE
{
"data": {
"score": "5x1",
"time": "15:10"
},
"to": "SAMPLE_SAMPLE"
}
This code works for Android.
Try following the bottom as an example. You can paste this code in http://phpfiddle.org/. Try to understand how the logic flow works and mess around with the code by adding the message you want to send, which is score and time. I found this code a while back when I was implementing push notifications. In order to make the code below work, you will need an authorization key as well as the device registration token. Good luck!
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Axxxxxxxxxxxxxxxxxx' );
$registrationIds = array("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$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, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>