2

几天来,我尝试从此处的 gcm 和 php 线程中实施许多解决方案,以使我的服务器将消息发送到 GCM 服务器,然后再发送到我的 Android 设备。我对 curl_exec($ch) 的调用一直返回 false。经过几天绞尽脑汁,阅读和搜索网络后,我似乎终于想通了。我不得不使用 GET 而不是 POST,我在这里找到了,而且我不必验证 SSL。(我不记得我在哪里找到的......)

我希望这可以帮助遇到同样问题的人。并且请,如果有人可以对此进行改进,那么他们的更正更受欢迎。

这是上面链接的线程所建议的:

$ch = curl_init();


// WRITE MESSAGE TO SEND TO JSON OBJECT
$message = '{"data":{"message":"here is the message","title":"message   title"},"registration_ids":["'. $reg . '","'. $reg2 . '"]}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);

curl_setopt($ch, CURLOPT_URL, $gcmurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',          
    'Authorization:key=' . $apiKey)
); 


$result = curl_exec($ch);
if(!$result > 0){
    echo "<p>An error has occurred.<p>";
    echo "<p>ERROR: $curl_error</p>";
}
else{
    $json_return = json_decode($result);
    echo var_dump($json_return);
    $info = curl_getinfo($ch);;

    echo "<p>httpcode: " . $info['http_code'] . "</p>";
}

curl_close($ch);

为了让我能够正常工作,我必须执行以下操作。

$ch = curl_init();

$message = '{"data":{"message":"here is the message","title":"message title"},"registration_ids":["'. $reg . '","'. $reg2 . '"]}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);

curl_setopt($ch, CURLOPT_URL, $gcmurl);

/*
* COMMENT OUT THE FOLLOWING LINE
*/
*//curl_setopt($ch, CURLOPT_POST, true);*
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',          
    'Authorization:key=' . $apiKey)
); 


/*
* ADD THESE 2 LINES
*/
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

$result = curl_exec($ch);
if(!$result > 0){
    echo "<p>An error has occurred.<p>";
    echo "<p>ERROR: $curl_error";
}
else{
    $json_return = json_decode($result);
    echo var_dump($json_return);
    $info = curl_getinfo($ch);;
}

curl_close($ch);
4

3 回答 3

0

你有没有;extension=php_curl.dll在你的 php.ini 中取消注释(远程或本地主机)

于 2013-10-02T13:39:52.153 回答
0
    $headers = array(
      'Accept: application/json',
      'Content-Type: application/json',
    );
    $data =  json_encode($input);//Your json data...
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

    switch($method)
    {
    case 'GET':
    curl_setopt($handle,CURLOPT_HTTPGET,true);
    break;

    case 'POST':
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);

    break;

    case 'PUT':
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    break;

    case 'DELETE':
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
    break;

    case 'FILE':
    curl_setopt($handle, CURLOPT_HEADER, 0);
    curl_setopt($handle, CURLOPT_VERBOSE, 1);
    curl_setopt($handle, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($handle, CURLOPT_POST, true);
    echo $data;
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    break;
    }

    $response = curl_exec($handle);
    $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

return  $response;  
于 2012-07-16T09:04:07.313 回答
0

这对我有用,因为它返回: {"multicast_id":8298406095978893272,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439084688510033%9bd2607ff9fd7ecd"}]} 但我无法在客户端收到通知。

<?php

class GCM {

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

    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {
        // include config
        require_once __DIR__ . '/db_config.php';

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

        // $fields = array(
        //     'registration_ids' => $registatoin_ids,
        //     'data' => $message,
        // );
        $fields = '{"to":"caFPICUdKKM:APA91bEJW2vtQHy5IQcOM88WlT5zazf-D9LExvOaSGgAOHqSkHBeHWP34KV1BEKLA9n932BrZXTCwJajug4kcX2LrURY1NJPb9V-vmis1Ra8bo2Zw2BgIIXrfoDbM42Ip6RN_ic1C6eU","data":{"title":"Testing","body":"Success","icon":"R.mipmap.ic_launcher"}}';

        $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));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);
        echo $result;
    }


}

?>
于 2015-08-09T01:48:35.373 回答