2012-07-08 73 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; 
    } 


} 

?>