0

I am trying to send push notification some android devices using the registration ids which is stored in database table.

Here is my code

  //message and title for push notification
  $msg=$_POST['message'];
  $title=$_POST['title'];
  $result=$connect->prepare("SELECT `push_id` FROM `user_devices`");
  $result->execute();
  //get registration id's
  $ids=$result->fetchAll(PDO::FETCH_COLUMN);
  $key="XXXXX";
  $url = 'https://android.googleapis.com/gcm/send';
  $fields = array(
                'registration_ids'  =>$ids,
                'data'              => array( "message" =>$msg,"title"=>$title,"soundname"=>"beep.wav" ),
                );
  $headers = array(
                    'Authorization: key=' . $key,
                    'Content-Type: application/json'
                );
  $ch = curl_init();
  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 ) );
  $res = curl_exec($ch);
  if($res === false)
     echo 'Curl failed ' .curl_error();
  }

I am getting curl Failed error message but didn't see anything from curl_error().

UPDATE

I am running this script from windows server which is hosted in azure cloud service. Now i am run this from linux based server its works fine,but it didnt work in windows server.

shamon shamsudeen
  • 5,466
  • 17
  • 64
  • 129

2 Answers2

1

You are probably getting error due to SSL (in your url https) issue. You have to configure your curl to handle secured request. You can find a hints from here: Configuring cURL for SSL

You are not getting the error msg because you need to print it like below:

echo 'Curl failed ' .curl_error($ch); // you are missing $ch here

Alternately you can run your curl with verbose enabled to see the things happening with your curl request!

curl_setopt( $ch, CURLOPT_VERBOSE, true );
Community
  • 1
  • 1
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
0

It was the problem of My SSL Certificate.

Solved using one line code curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); is placed in after curl_init.

NOTE

This is a temporary solution.Actual way is generate an SSl certificate.

shamon shamsudeen
  • 5,466
  • 17
  • 64
  • 129