【问题标题】:Access denied from curl on remoter server从远程服务器上的 curl 拒绝访问
【发布时间】:2013-08-31 09:37:37
【问题描述】:

我正在为 android 开发推送通知服务,并为此遵循了一些教程。我试图在 localhost 中实现该技术并成功了。但是,当我尝试使用远程服务器时,它给了我错误“Curl 失败:无法连接到:权限被拒绝”。我不知道它有什么问题。我尝试寻找解决方案,但没有一个有效。这是我的步骤。

我为云消息传递创建了一个名为 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,然后显示该错误。请帮助我。

【问题讨论】:

    标签: php android curl expressionengine google-cloud-messaging


    【解决方案1】:

    您应该在 HTTP 请求的标头中包含 API 密钥(从您的 Google API 项目获得):

    Authorization: key=YOUR_API_KEY

    这就是授权您从服务器向您的应用发送 GCM 消息的原因。

    编辑,现在您添加了GCM.php 代码:

    为什么禁用 SSL?

    // 暂时禁用 SSL 证书支持

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    我不了解 PHP,但在其他 PHP GCM 示例中没有看到这一行,例如 this

    你在哪里定义GOOGLE_API_KEY

    【讨论】:

    • :我在 GCM php 文件中包含了那个。
    • @user2522586 在这种情况下,您应该在问题中包含您的GCM.php 代码,以便我们查看那里是否缺少任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多