简体   繁体   English

使用PHP发送推式通知到Android:curl失败

[英]Sending Push notification to android Using PHP :curl failed

I am trying to send push notification some android devices using the registration ids which is stored in database table. 我正在尝试使用存储在数据库表中的注册ID向某些Android设备发送推送通知。

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(). 我收到curl Failed错误消息,但未从curl_error()看到任何内容。

UPDATE 更新

I am running this script from windows server which is hosted in azure cloud service. 我正在从Azure云服务中托管的Windows Server运行此脚本。 Now i am run this from linux based server its works fine,but it didnt work in windows server. 现在我从基于linux的服务器上运行它,它的工作正常,但在Windows服务器中却不起作用。

You are probably getting error due to SSL (in your url https) issue. 由于SSL(在您的网址https中)问题,您可能会收到错误消息。 You have to configure your curl to handle secured request. 您必须配置curl以处理受保护的请求。 You can find a hints from here: Configuring cURL for SSL 您可以从此处找到提示: 为SSL配置cURL

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,以查看curl请求中发生的事情!

curl_setopt( $ch, CURLOPT_VERBOSE, true );

It was the problem of My SSL Certificate. 这是我的SSL证书的问题。

Solved using one line code curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 使用一行代码curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); is placed in after curl_init. 放在curl_init之后。

NOTE 注意

This is a temporary solution.Actual way is generate an SSl certificate. 这是一个临时解决方案。实际方法是生成SS1证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM