2012-07-08 51 views
2

几天后,我试着从gcm & php thread here实施一些解决方案,让我的服务器向GCM服务器发送消息,然后发送到我的Android设备。我对curl_exec($ ch)的调用保持返回false。几天后,我的大脑,阅读和搜索网络,似乎我终于明白了。我不得不使用GET而不是POST,我发现here,我不得不验证SSL。 (我不记得我在哪里发现的......)来自PHP的PHP JSON GCM请求

我希望这可以帮助遇到同样问题的人。如果有人能够改善这一点,他们的更正更受欢迎。

这是被链接到上面的线索提示:

$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); 
+0

我试过这个,但它不工作。参见:http://stackoverflow.com/questions/11396177/gcm-sending-with-curl-php – Klaasvaak 2012-07-09 13:49:44

回答

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; 
0

你有没有在你的php.ini uncommentted ;extension=php_curl.dll(远程或本地主机)

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; 
    } 


} 

?>