1

I am working on a php script to send the notification to the CGM server and I am working from this example:

public function send_notification($registatoin_ids, $message) { // include config include_once './config.php';

    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    // Open connection
    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    echo $result;
}

But I am not certain what the values should be for the variables: CURLOPT_POSTFIELDS , CURLOPT_SSL_VERIFYPEER , CURLOPT_RETURNTRANSFER , CURLOPT_HOST , CURLOPT_URL

Would anyone happen to know what the values for these should be?

Thank you!

Genadinik
  • 16,715
  • 59
  • 170
  • 277

1 Answers1

2

" CURLOPT_POSTFIELDS " this field is required to push number of fields in json encoding format. "CURLOPT_SSL_VERIFYPEER" this field is required if your third party server is HTTPS over SSL. "CURLOPT_RETURNTRANSFER" This field gives you reply from gcm server. "CURLOPT_HOST , CURLOPT_URL" this field is for HOST name and URL you have to define of your third party server.

That is de code that I have implemented :

<?php
$apiKey = "Your Api Key"; 
$reg_id = array("Your registration ID that we have get from device");
$registrationIDs = $reg_id;

// Message to be sent
$message = $_REQUEST['message']; 

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
          'registration_ids' => $registrationIDs,
          'data' => array( "message" => $message ),
           );
$headers = array(
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
         );

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
?>

As per my experience this links will help you :

GCM with PHP (Google Cloud Messaging)

http://www.sherif.mobi/2012/07/gcm-php-push-server.html

https://groups.google.com/forum/#!topic/android-gcm/hjk5PUYlTp0

Community
  • 1
  • 1
Jai
  • 1,874
  • 2
  • 19
  • 41
  • @Jsi been looking through these but can not find specific values for these variables. By any chance would you have examples of the values? – Genadinik Oct 20 '12 at 04:44
  • You have to just pass on values according to my above description. I have defined that what each field required to pass on? Now let me know your further problem. Still are you confuse? – Jai Oct 20 '12 at 04:46
  • ye a bit. For the first item, I guess it is easy since it is the number of things I am passing in JSON, so it is something like 2. But for the last 2 fields, I am really not sure. – Genadinik Oct 20 '12 at 04:52
  • I have updated my working code. You have to just use few things as I have commented!! – Jai Oct 20 '12 at 04:58
  • thank you, but where did you define the values for those fields? – Genadinik Oct 20 '12 at 05:03
  • curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); You need not to worry about those fields..other field are I have already define with thier values.. – Jai Oct 20 '12 at 05:06
  • @Genadinik : Now you got perfectly what you have to do? – Jai Oct 20 '12 at 05:23
  • kind of.....trying...I just still have to figure out the exact valies for the CURLOPT_SSL_VERIFYPEER and other consts like that. But I think I have enough guidance on what those values should be – Genadinik Oct 20 '12 at 05:37