解码从firebase返回json

问题描述 投票:1回答:1

我想从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个回答
0
投票

只有你的第二点的部分答案,访问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
© www.soinside.com 2019 - 2024. All rights reserved.