【问题标题】:Google GCM server returns 404 errorGoogle GCM 服务器返回 404 错误
【发布时间】:2012-07-12 11:46:52
【问题描述】:

我想通过 PHP 将消息从我的服务器发送到电话。 这是我的代码:

    $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。结果我得到(在 $result 变量中):

<HTML>
<HEAD>
<TITLE>Not Found</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Not Found</H1>
<H2>Error 404</H2>
</BODY>
</HTML>

我不知道为什么。任何人都可以帮忙吗?文档中没有提到 404 代码,所以我真的不知道发生了什么。

【问题讨论】:

  • 404 表示您尝试连接的服务 url 不存在。 en.wikipedia.org/wiki/HTTP_404
  • 嗯...我知道,但我真的不知道为什么它在我的情况下这样说。我从link 获得网址,所以应该不错,不是吗?我真的不知道为什么我得到 404 :(

标签: php android google-cloud-messaging


【解决方案1】:

哦,我完全忘记了这个问题,对不起。我已经找出问题所在了。 早些时候,我使用我之前发布的代码通过 C2DM 发送消息。我只做了一些改进以将其调整为 GCM。在某些情况下,变量之一 ($msgExtra) 可能等于 null。这是预期的行为,并且与 C2DM 一起工作得很好。 不幸的是,当您尝试通过 GCM 在 JSON 中传递 null 时,您会收到 404 错误,尽管 GCM 文档对此只字未提... 因此,只要您不尝试发送空值,我发布的代码就很好。 我的问题的解决方案是用“”之类的东西替换空值。 再一次 - 抱歉我刚刚发布它,我完全忘记了这个问题。

【讨论】:

  • 感谢您发布答案。我相信这会帮助很多人。
  • 确认这对我有帮助。就我而言,这是 GCM 中的重大缺陷;如果在您发送一个有效的序列化值供您的应用程序使用时它会像这样翻转,那么它并不真正支持 JSON。某些字段为 null(而不是 0 或 '' 或另一个 null 模仿者)的信息可能正是您需要推送到您的应用程序的信息。
  • 经过数小时的调试,我在这里找到了答案。该死的 GCM,你应该编写正确的 JSON 解析器。
【解决方案2】:

从 Google API 控制台生成浏览器 API 密钥,并使用它代替“授权”标头中的服务器密钥。一旦你这样做了,这个错误就会消失。

这是由 GCM 文档中的一个严重错误造成的,该错误指出您应该在授权标头中使用服务器密钥(如 Over here 所写)。

【讨论】:

  • 不幸的是它没有帮助。我在 StackOverflow 上读过这个错误,所以我使用了“浏览器应用程序的密钥(带有引用者)”,它是在我打开 GCM 时自动生成的。在您发布之后,我生成了新密钥,但它也不起作用。仍然是 404。但感谢您的帮助
【解决方案3】:

可能已经为您解决了。但这是我在设备上测试的代码的工作版本。

<?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 中的服务器端实现的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多