1

来自 PHP 的 Google Cloud 消息传递调用。我已经用很多人说的错误代码进行了很多搜索,如果卸载应用程序,您可能会收到此错误。但在我的情况下,应用程序没有被卸载。

我想从 php 发送推送通知。

我的 php 代码看起来像 -

include "../db_con.php";
// Replace with the real server API key from Google APIs
$apiKey = "xxx";

// Message to be sent
$message = "GET DEVICE INFO NOW";

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';


if(isset($_REQUEST['imei_no']) && $_REQUEST['imei_no']!='')
{
$sql = "SELECT gcmId FROM device_tracker_device_master_tbl WHERE deviceId = ". $_REQUEST["imei_no"] ."";
$result = mysql_query($sql);

if (mysql_num_rows($result)>0) 
{
    while($row = mysql_fetch_assoc($result))  
    {
        // Replace with the real client registration IDs
        $registrationIDs = array( $row["gcmId"] );

        $fields = array(
            'registration_ids' => $registrationIDs,
            'data' => array( "message" => $message ),
        );
        $headers = array(
            'Authorization: key=' . $apiKey,
            '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);
        //curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // curl_setopt($ch, CURLOPT_POST, true);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));

        // Execute post
        $result = curl_exec($ch);

        // Close connection
        curl_close($ch);
        // print the result if you really need to print else neglate thi
        echo $result;
        //print_r($result);
        //var_dump($result);
    }
} 
else 
{
    echo "Sorry no device found with this IMEI !!";
}
}

错误看起来像 -

在此处输入图像描述

4

1 回答 1

2

非常感谢@e4c5,我找到了解决方案

在我的服务器 CURL 没有安装我安装使用

sudo apt-get install php5-curl

然后重述了 apache2,现在一切正常。

sudo /etc/init.d/apache2 restart
于 2015-09-08T20:48:51.953 回答