0

I'am trying to send notification message to android device using the code below the out put form the result is

Curl failed: Failed to connect to android.googleapis.com port 443: 
Connection timed out

my server is amazon ec2.

my code :

public function send_notification($registatoin_ids, $message) {
    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );
    $headers[] = 'Content-Type:application/json';
    $headers[] = 'Authorization:key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    // 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_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_HEADER, 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;
}

I just tried the code on another server it's working

{"multicast_id":5322863685330856855,"success":1,"failure":0,"canonical_ids":0,
"results":[{"message_id":"0:1401820119301203%a5dcde78f9fd7ecd"}]}

so i think there is something wrong with amazon ec2 settings

Khaled Hasania
  • 335
  • 6
  • 14
  • I have never used Amazon EC2, but I'm pretty sure the 443 port is opened. Test this : http://stackoverflow.com/a/11253231/1788704 and this : http://stackoverflow.com/a/21487471/1788704 . It works for me. Thus you can remove `curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false)` – kmas Jun 03 '14 at 15:34
  • I tried the code in the first link it gives me the same result. – Khaled Hasania Jun 03 '14 at 16:04
  • Sorry, it seems I was wrong : http://stackoverflow.com/questions/15679227/how-can-i-open-port-2195-and-443-on-my-amazon-ec2-server – kmas Jun 03 '14 at 16:15
  • I checked these ports both are opened – Khaled Hasania Jun 03 '14 at 17:55
  • Thank you @kmas the port 443 was not open – Khaled Hasania Jun 03 '14 at 21:22

1 Answers1

1

I figured out the problem my code was correct, port 443 was not open on the sever

Khaled Hasania
  • 335
  • 6
  • 14