0

I'm working on push notification service for android and followed some tutorial for that. I tried to implement the technique in localhost and succeeded. But While I tried with my remote server it is giving me the error "Curl failed: Failed to connect to : Permission denied". I don't know whats the problem with it. I tried looking for solution, but non of them worked. Here is the step how I did.

I created a database called GCM for cloud messaging Added the following line of code in publish_content.php page

$username = "given";
            $password = "given";
            $hostname = "localhost"; 

            //connection to the database
            $dbhandle = mysql_connect($hostname, $username, $password) 
             or die("Unable to connect to MySQL");
//          echo "Connected to MySQL<br>";

            //select a database to work with
            $selected = mysql_select_db("gcm",$dbhandle) 
              or die("Could not select examples");


            //execute the SQL query and return records
            $result = mysql_query("SELECT gcm_regid FROM gcm_users");
            $regIds = array();
            include_once 'GCM.php';
            //fetch tha data from the database 
            while ($row = mysql_fetch_array($result)) {
                array_push($regIds, $row['gcm_regid']);

            }

            foreach($regIds as $item) {
                $gcm = new GCM();
                $registatoin_ids = array($item);
                    $message = array("price" => $message1);
                $result = $gcm->send_notification($registatoin_ids, $message);
            }

            //close the connection
            mysql_close($dbhandle);

GCM.php

<?php

class GCM {

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

    }

    /**
     * Sending Push Notification
     */
    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;
    }

}

?>

It connects to mysql and after that it is showing that error. Please help me on this.

Eran
  • 387,369
  • 54
  • 702
  • 768
user2522586
  • 97
  • 1
  • 10

1 Answers1

1

You should include the API Key (obtained from your Google API Project) in the headers of the HTTP request:

Authorization: key=YOUR_API_KEY

That's what authorizes you to send a GCM message from your server to your app.

EDIT, now that you added the GCM.php code:

Why do you disable SSL?

// Disabling SSL Certificate support temporarly

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

I don't know PHP, but I don't see this line in other PHP GCM samples, such as this.

And where do you define GOOGLE_API_KEY?

Community
  • 1
  • 1
Eran
  • 387,369
  • 54
  • 702
  • 768