【问题标题】:decode return json from firebase从firebase解码返回json
【发布时间】:2017-12-16 08:14:27
【问题描述】:

我想从 url-> "https://fcm.googleapis.com/fcm/send" 解码这个 json 但不幸的是我没有成功,我真的不知道该怎么办请帮助我,在下面你看到我的代码:

    function decodeJSON($ids)
{
    $MyString = sendFCMToMultiple($ids);
    echo "return:" . $MyString . PHP_EOL;
    //$MyDecode = json_decode($MyString, false, 512, JSON_BIGINT_AS_STRING);
    //$MyDecode = json_decode($MyString, true, 512, JSON_BIGINT_AS_STRING);
    $MyDecode = json_decode($MyString);
    $MultiCastID = $MyDecode->multicast_id;
    $Success = $MyDecode->success;
    $Failure = $MyDecode->failure;
    $CanonicalIDS = $MyDecode->canonical_ids;
    $Results = $MyDecode->results;
    //echo 'Var MultiCastID=' . var_dump($MultiCastID) . PHP_EOL;
    echo 'MultiCastID to Str=' . number_format($MultiCastID, 0, '.', '') . PHP_EOL;
    echo 'MultiCastID=' . $MultiCastID . PHP_EOL;
    echo 'Success=' . $Success . PHP_EOL;
    echo 'Failure=' . $Failure . PHP_EOL;
    echo 'CanonicalIDS=' . $CanonicalIDS . PHP_EOL;
    echo 'Results=' . $Results . PHP_EOL;

    echo '=================================================================' . PHP_EOL;
}

这是来自 fcm url 的原始和主要 json:

{
  "multicast_id": 7640049088650537742,
  "success": 2,
  "failure": 2,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    },
    {
      "error": "NotRegistered"
    },
    {
      "message_id": "0:1513401047065944%93a06d80f9fd7ecd"
    },
    {
      "message_id": "0:1513401047066546%93a06d80f9fd7ecd"
    }
  ]
}

这是返回并从我的代码中显示:

{"multicast_id":7640049088650537742,"success":2,"failure":2,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"0:1513401047065944%93a06d80f9fd7ecd"},{"message_id":"0:1513401047066546%93a06d80f9fd7ecd"}]}
MultiCastID to Str=7640049088650537984
MultiCastID=7.6400490886505E+18
Success=2
Failure=2
CanonicalIDS=0
Results=
=================================================================

还有我的问题:

首先:我不能使用 json 字符串来 json_decode($MyString, false, 512, JSON_BIGINT_AS_STRING) 并为所有键返回空值;这就是为什么我说我想要 multicast_id 键的字符串值或整数类型,但 json_decode 本身自动大整数浮点数,我不喜欢它,我想要整数和/或字符串值,尽管我使用了第 5 行和第 6 行并且因为评论在代码中,但不是真的。 第二:结果键数组对我来说非常重要,因为我明白谁没有收到,谁收到了,为什么?谢谢。

【问题讨论】:

标签: php json firebase push-notification decode


【解决方案1】:

您的第二点只是部分答案,以访问 json 中的结果(在 php 5.6 中测试):

foreach ($MyDecode->results as $result) {
    if (isset($result->error)) {
        echo 'Found error: ' . $result->error . PHP_EOL;
    } elseif (isset($result->message_id)) {
        echo 'Found message id: ' . $result->message_id . PHP_EOL;
    }
}

输出:

Found error: NotRegistered
Found error: NotRegistered
Found message id: 0:1513401047065944%93a06d80f9fd7ecd
Found message id: 0:1513401047066546%93a06d80f9fd7ecd

【讨论】:

  • 我测试了你的代码,它的工作就像魅力!我必须对你说的一件事我还不能从 multicast_id 键解析为字符串值,即使使用你建议的解决方案......?? !!!非常感谢你的胸罩!!!?? @astrangeloop 只是
  • 基于stackoverflow.com/questions/19520487/…中的答案,您可以在解码前使用preg_replace将bigint转换为json中的字符串:$MyString = sendFCMToMultiple($ids);$MyString = preg_replace('/("multicast_id":\s*)(\d+)/', '\1"\2"', $MyString);$MyDecode = json_decode($MyString);var_dump($MyDecode->multicast_id);给:@987654328 @
  • 真的真的很感谢,祝你一切顺利,希望每个舞台生活永远好运文胸!!!谢谢!!! ???????????????​​pan>
猜你喜欢
  • 1970-01-01
  • 2019-05-19
  • 2019-05-10
  • 2021-01-14
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多