3

i am trying to create a push notification application which will send messages from the server to the users device.

for now i get the registration ids but when i run the curl_exec function i get the following error:

Curl failed: couldn't connect to host

i don't know why is that and i made some checks and when i try to change the URL to "google.com" is pass. from my browser if i try to reach the original URL its redirect me to : "https://developers.google.com/cloud-messaging/" this is the php code:

    <?php
class GCM {

    function __construct() {

    }
    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {
        // include config
        include_once 'connection.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_IPRESOLVE, CURL_IPRESOLVE_V4 );
        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;
    }
}
?>
Matan Tubul
  • 774
  • 3
  • 11
  • 33

1 Answers1

1

i found that my server containing a firewall which blocking my outgoing connections. so i just open it and its working now. this is the solution which helped me to solve the problem with link to the relevant answer.

CURL error code 7 (CURLE_COULDNT_CONNECT) is very explicit ... it means Failed to connect() to host or proxy.

The following code would work on any system:

$ch = curl_init("http://google.com");    // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);

If you can not see google page then .. your URL is wrong or you have some firewall or restriction issue

this link help me to resolve this issue

Community
  • 1
  • 1
Matan Tubul
  • 774
  • 3
  • 11
  • 33