php - curl_exec 使用谷歌云消息传递返回 null

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

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

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

jdata 为空,消息未发送。我不知道是什么问题。

最佳答案

很难判断哪里出了问题,您至少应该从 curl 执行中抛出错误:

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

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

return $httpCode;

关于php - curl_exec 使用谷歌云消息传递返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23032171/

相关文章:

android - 未生成 FCM 推送通知 token

php - 无法将新行插入到 postgres 数据库表中?

php - MySQL PHP 查询问题

php安装脚本

Android - 以编程方式检查互联网连接并在未连接时显示对话框

android - 谷歌地图 API v2。安卓

php - 将 proc_open() 获取的管道重定向到文件,以处理剩余的过程持续时间

android - 请勿打扰模式 - 始终静音来电

ios - 上传 APNS 证书的问题 Firebase

azure - Windows 8 推送通知,无需将应用程序关联到 Windows 应用商店