1

Google Cloud Messaging Call from PHP. I have searched a lot with the error code many people are saying in case of application uninstalled you can get this error. But in my case application not been uninstalled.

I am tying to send push notification from php.

My php code looks like -

include "../db_con.php";
// Replace with the real server API key from Google APIs
$apiKey = "xxx";

// Message to be sent
$message = "GET DEVICE INFO NOW";

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


if(isset($_REQUEST['imei_no']) && $_REQUEST['imei_no']!='')
{
$sql = "SELECT gcmId FROM device_tracker_device_master_tbl WHERE deviceId = ". $_REQUEST["imei_no"] ."";
$result = mysql_query($sql);

if (mysql_num_rows($result)>0) 
{
    while($row = mysql_fetch_assoc($result))  
    {
        // Replace with the real client registration IDs
        $registrationIDs = array( $row["gcmId"] );

        $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);
        // print the result if you really need to print else neglate thi
        echo $result;
        //print_r($result);
        //var_dump($result);
    }
} 
else 
{
    echo "Sorry no device found with this IMEI !!";
}
}

Error looks like -

enter image description here

A J
  • 4,542
  • 5
  • 50
  • 80
  • If you are sending many messages you can get 5xx errors, guidelines mention to use an [exponential backoff](https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload#best-practices) – AlexBcn Sep 08 '15 at 09:06
  • As you can see I am calling this service only once. So as per my logic I am calling once only. – A J Sep 08 '15 at 09:10
  • I have added error log also please check @exponential backoff – A J Sep 08 '15 at 09:39
  • Are you sure that this error is returned by Google's server and not your php code? – Ilya Tretyakov Sep 08 '15 at 10:13
  • Yes If I comment FROM - // Open connection TO - echo $result; and put a echo "DONE"; I am getting that. – A J Sep 08 '15 at 10:41
  • That doesn't rule out a error in your code resulting in a 500 at your end. Do check the PHP error log – e4c5 Sep 08 '15 at 14:47
  • Thanks for your reply @e4c5 I just checked the php log I got the exact line it's line no 39 mean $ch = curl_init(); ANY CLUE ? – A J Sep 08 '15 at 20:42

1 Answers1

2

I found the solution many many thanks to @e4c5

In my server CURL was not installed I installed that using

sudo apt-get install php5-curl

Then restated the apache2 every things is working now.

sudo /etc/init.d/apache2 restart
A J
  • 4,542
  • 5
  • 50
  • 80