2

For days I tried to implement a number of solutions from the gcm & php thread here to get my server to send a message to the GCM server and then to my Android device. My call to curl_exec($ch) kept returning false. After a few days of racking my brain, reading and searching the web it seems I finally figured it out. I had to use GET instead of POST, I found that here, and I had to NOT verify the SSL. (I can't remember where I found that...)

I hope this helps someone who's having the same problem. And please, if anyone can improve upon this their corrections are more then welcome.

This is what was suggested by the thread linked to above:

$ch = curl_init();


// WRITE MESSAGE TO SEND TO JSON OBJECT
$message = '{"data":{"message":"here is the message","title":"message   title"},"registration_ids":["'. $reg . '","'. $reg2 . '"]}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);

curl_setopt($ch, CURLOPT_URL, $gcmurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',          
    'Authorization:key=' . $apiKey)
); 


$result = curl_exec($ch);
if(!$result > 0){
    echo "<p>An error has occurred.<p>";
    echo "<p>ERROR: $curl_error</p>";
}
else{
    $json_return = json_decode($result);
    echo var_dump($json_return);
    $info = curl_getinfo($ch);;

    echo "<p>httpcode: " . $info['http_code'] . "</p>";
}

curl_close($ch);

For me to get this working I have to implement the following.

$ch = curl_init();

$message = '{"data":{"message":"here is the message","title":"message title"},"registration_ids":["'. $reg . '","'. $reg2 . '"]}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);

curl_setopt($ch, CURLOPT_URL, $gcmurl);

/*
* COMMENT OUT THE FOLLOWING LINE
*/
*//curl_setopt($ch, CURLOPT_POST, true);*
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',          
    'Authorization:key=' . $apiKey)
); 


/*
* ADD THESE 2 LINES
*/
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

$result = curl_exec($ch);
if(!$result > 0){
    echo "<p>An error has occurred.<p>";
    echo "<p>ERROR: $curl_error";
}
else{
    $json_return = json_decode($result);
    echo var_dump($json_return);
    $info = curl_getinfo($ch);;
}

curl_close($ch);
Community
  • 1
  • 1
eimmer
  • 319
  • 1
  • 3
  • 13
  • I'm trying this aswell, but it isn't working. See: http://stackoverflow.com/questions/11396177/gcm-sending-with-curl-php – Klaasvaak Jul 09 '12 at 13:49

3 Answers3

0
    $headers = array(
      'Accept: application/json',
      'Content-Type: application/json',
    );
    $data =  json_encode($input);//Your json data...
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

    switch($method)
    {
    case 'GET':
    curl_setopt($handle,CURLOPT_HTTPGET,true);
    break;

    case 'POST':
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);

    break;

    case 'PUT':
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    break;

    case 'DELETE':
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
    break;

    case 'FILE':
    curl_setopt($handle, CURLOPT_HEADER, 0);
    curl_setopt($handle, CURLOPT_VERBOSE, 1);
    curl_setopt($handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($handle, CURLOPT_POST, true);
    echo $data;
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    break;
    }

    $response = curl_exec($handle);
    $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

return  $response;  
j0k
  • 22,600
  • 28
  • 79
  • 90
Shiju Shaji
  • 1,682
  • 17
  • 24
0

Have you uncommentted ;extension=php_curl.dll in your php.ini(remote or localhost)

macio.Jun
  • 9,647
  • 1
  • 45
  • 41
0

This worked for me cause it returns : {"multicast_id":8298406095978893272,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439084688510033%9bd2607ff9fd7ecd"}]} but I can't receive the notification on the client side.

<?php

class GCM {

    //put your code here
    // constructor
    function __construct() {

    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {
        // include config
        require_once __DIR__ . '/db_config.php';

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

        // $fields = array(
        //     'registration_ids' => $registatoin_ids,
        //     'data' => $message,
        // );
        $fields = '{"to":"caFPICUdKKM:APA91bEJW2vtQHy5IQcOM88WlT5zazf-D9LExvOaSGgAOHqSkHBeHWP34KV1BEKLA9n932BrZXTCwJajug4kcX2LrURY1NJPb9V-vmis1Ra8bo2Zw2BgIIXrfoDbM42Ip6RN_ic1C6eU","data":{"title":"Testing","body":"Success","icon":"R.mipmap.ic_launcher"}}';

        $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));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

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

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


}

?>
Jaye
  • 132
  • 1
  • 10