Google GCM服务器返回404错误
我想通过PHP将我的服务器发送的消息发送到手机。 这里是我的代码:Google GCM服务器返回404错误
$apiKey = "AIxxxxxxxxxxxxxxxxxxxx";
$registrationIDs = array($c2dmId);
$url = "https://android.googleapis.com/gcm/send";
$headers = array(
'Authorization: key='.$apiKey,
'Content-Type: application/json'
);
$fields = array(
'collapse_key' => $collapseKey,
'data' => array(
"type" => $msgType,
"extra" => $msgExtra,
"uuid" => $uuid,
"user_id" => $userId),
'registration_ids' => $registrationIDs,
);
print (json_encode($fields));
echo "<br/>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
$resultInfo = curl_getinfo($ch);
echo "resultinfo: $resultInfo <br>";
foreach ($resultInfo as $key => $value) {
echo "$key => $value <br>";
}
curl_close($ch);
die ("Result: $result");
其中$ c2dmId只是registrationId我发送到服务器的电话。因此,我得到(在$结果变量):
<HTML>
<HEAD>
<TITLE>Not Found</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Not Found</H1>
<H2>Error 404</H2>
</BODY>
</HTML>
我不知道为什么。谁能帮忙?文档没有说404代码的任何内容,所以我真的不知道发生了什么。
哦,我完全忘了这个问题,抱歉了点。我已经找出问题所在。 早些时候,我使用了之前发布的通过C2DM发送消息的代码。我只做了一些改进,将其调整为GCM。其中一个变量($ msgExtra)在某些情况下可能等于null。这是预期的行为,并与C2DM它工作得很好。 不幸的是,当你尝试通过GCM在JSON传递空你得到404错误,尽管GCM文件没有提到这... 所以我贴的代码是很好的,只要你不尝试发送无效。 我的问题的解决方案是用类似“”的东西来替换空值。 再次 - 抱歉,我刚刚发布它,我完全忘了这个问题。
感谢张贴答案。我相信这将有助于人很多。 – dnkoutso 2012-08-10 22:57:33
确认这有助于这是GCM中的一个重大缺陷,就我而言,它并不真正支持JSON,如果它在你发送一个有效的序列化的值给你的应用程序使用时这样翻转,某些字段为空(而不是0或''或另一个空模仿者)可能正是你需要推送到你的应用程序的东西。 – jeffcook2150 2012-09-10 22:31:42
经过几个小时的调试,我在这里找到了我的答案,该死的GCM,你应该编写适当的JSON解析器。 – est 2012-09-21 01:21:43
可它已经解决了you.But这里是你的代码,我在测试设备的工作版本。
<?php
$apiKey = "AI.....";
$registrationIDs = array("You registration key" );
$url = "https://android.googleapis.com/gcm/send";
$headers = array(
'Authorization: key='.$apiKey,
'Content-Type: application/json'
);
$msgType = "hello";
$msgExtra = "rock";
$uuid = '1234';
$userId = '5678';
/* data you need to define message you want to send in APP.For ex: here myssg in what i am fetching at client side, so in notification it will show hello. You can change accordingly. */
$fields = array(
'collapse_key' => $collapseKey,
'data' => array(
"mymsg" => $msgType,
"extra" => $msgExtra,
"uuid" => $uuid,
"user_id" => $userId),
'registration_ids' => $registrationIDs,
);
print (json_encode($fields));
echo "<br/>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
$resultInfo = curl_getinfo($ch);
echo "resultinfo: $resultInfo <br>";
foreach ($resultInfo as $key => $value) {
echo "$key => $value <br>";
}
curl_close($ch);
die ("Result: $result");
?>
这不会给你404错误。希望这会帮助你或者一些在PHP中寻找服务器端的实现。
404表示您尝试连接的服务url不存在。 http://en.wikipedia.org/wiki/HTTP_404 – sunil 2012-07-12 12:10:21
嗯...我知道,但我真的不知道它为什么说这在我的情况。我从[链接](http://developer.android.com/guide/google/gcm/gcm.html)获得网址,所以它应该是好的,不是吗?我真的不知道为什么我会得到404 :( – anula 2012-07-12 12:48:48