【问题标题】:curl_exec return null with google cloud messagingcurl_exec 使用谷歌云消息返回 null
【发布时间】:2014-04-12 15:12:34
【问题描述】:
function sendMessageToPhone($collapseKey, $messageText, $username)
 {
   include_once 'users.php';
   $user = new users();
   $data = $user->getUser($username); 

   $apiKey = 'MY_API_KEY';

   $userIdentificador = $data["gcmcode"];

   $headers = array('Content-Type: application/json',
                    'Authorization:key=' . $apiKey);

   $info = array('message' => $messageText);

   $data = array(
     'registration_ids' => $userIdentificador,
     'collapse_key' => $collapseKey,
     'data' => $info);

   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

   $response = curl_exec($ch);
   $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

   curl_close($ch);
   return $response;    //Return null???

 }

行:

$responde = curl_exec($ch); 

返回空值。为什么?在我的 Android 代码中:

JSONObject jdata = PostServer.getserverdata(nameValuePairs, Server.getIPServer());

jdata 为 null 且不发送消息。不知道是什么问题。

【问题讨论】:

    标签: php android push-notification google-cloud-messaging


    【解决方案1】:

    很难判断出了什么问题,你至少应该从curl 执行中抛出错误:

    $response= curl_exec($ch);
    if ($response=== FALSE) {
      die('error curl: ' . curl_error($ch));
    }
    

    那么返回http代码本身并找出它的值会更有意义:

    return $httpCode;
    

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 2016-04-21
      相关资源
      最近更新 更多