curl_exec通过谷歌云消息传递返回null

时间:2014-04-12 15:12:34

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

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

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

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

jdata为空,且邮件未发送。我不知道问题所在。

1 个答案:

答案 0 :(得分:1)

告诉错误是什么很棘手,你至少应该从curl执行中抛出错误:

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

然后返回http代码本身并找出其值更有意义:

return $httpCode;