-->

访问从更远的服务器上的卷曲否认(Access denied from curl on remoter

2019-10-29 20:06发布

我工作的推送通知服务,为Android和遵循一些教程说。 我试图实现本地主机的技术和成功。 但是,当我与我的远程服务器这是给我的错误尝试“卷曲失败:无法连接到:权限被拒绝”。 我不知道什么是它的问题。 我试图寻找解决方案,但他们的工作不。 这里是我做的步骤。

我创建了一个名为GCM云消息添加下面的代码行publish_content.php页面数据库

$username = "given";
            $password = "given";
            $hostname = "localhost"; 

            //connection to the database
            $dbhandle = mysql_connect($hostname, $username, $password) 
             or die("Unable to connect to MySQL");
//          echo "Connected to MySQL<br>";

            //select a database to work with
            $selected = mysql_select_db("gcm",$dbhandle) 
              or die("Could not select examples");


            //execute the SQL query and return records
            $result = mysql_query("SELECT gcm_regid FROM gcm_users");
            $regIds = array();
            include_once 'GCM.php';
            //fetch tha data from the database 
            while ($row = mysql_fetch_array($result)) {
                array_push($regIds, $row['gcm_regid']);

            }

            foreach($regIds as $item) {
                $gcm = new GCM();
                $registatoin_ids = array($item);
                    $message = array("price" => $message1);
                $result = $gcm->send_notification($registatoin_ids, $message);
            }

            //close the connection
            mysql_close($dbhandle);

GCM.php

<?php

class GCM {

    //put your code here
    // constructor
    function __construct() {

    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {
        // include config
        include_once './config.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_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;
    }

}

?>

它连接到MySQL,它显示的错误之后。 请帮我在这。

Answer 1:

你应该包括在HTTP请求的报头中的API密钥(来自谷歌API项目获得):

Authorization: key=YOUR_API_KEY

这就是授权您从您的服务器发送GCM消息,您的应用程序。

编辑,现在你加入了GCM.php代码:

你为什么禁用SSL?

//禁用SSL证书支持,即暂时

curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);

我不知道PHP,但我没有看到其他PHP GCM样品,如在此行此 。

和你在哪里定义GOOGLE_API_KEY



文章来源: Access denied from curl on remoter server