处理来自第 3 方的无效 Json 响应

Handle invalid Json responce from 3rd Paty

在网站上工作,我正在访问 Steam API 到 return 用户的 CSGO 物品清单

这是我打的url

$response = $this->guzzle->request('GET', "http://steamcommunity.com/profiles/{$communityId}/inventory/json/730/2");

所以在 dev/locally 中,它与以下

一起工作正常
foreach (json_decode($response->getBody()->getContents(), true)['rgDescriptions'] as $item) {
        $inventory[$item['name']] = $item;
    }

return $inventory;

但是当我更新我的网站时,它抱怨我的 foreach 循环的参数无效 - 在 var 转储之后 - ($response->getBody()->getContents()) - 仅实时站点 获得 returned 的 json 数据包含奇怪的古怪东西 - 我通常是 json 的新手,但它包含如下内容

"""
00006000\r\n
{"success":true,"rgInventory":{"8161072596":{"id":"8161072596","classid":"1560432042","instanceid":"480085569","amount":"1","pos":1},"8159918836":

和这样的结束块

{"internal_name":"normal","name":"Normal","category":"Quality","category_name":"Category"},{"internal_name":"Rarity_Ancient","name":"Extraordinary","category":"Rarity","color":"eb4b4b","category_name":"Quality"}]}},"more":false,"more_start":false}\r\n
00000000\r\n
\r\n
"""

所以这当然是我的 foreach 中断的原因,因为我的 json_decode 响应将 returning null 而不是 assoc 数组? (查看底部的完整功能!)

虽然我不确定,但我很确定这些是换行符?它相当烦人我不确定我是否遗漏了我这边的某些东西或者它来自 Steam 的响应,尽管我看不到我在开发时工作正常时的情况?

我在研究中也遇到了 magic_quotes,但我读到它们在 php 5.4 中被贬值了?我是 运行 5.6,所以我只能认为服务器端一定还缺少其他东西?

下面是我的辅助函数,供参考

public function getCSGOInventory($communityId)
{
    $response = $this->guzzle->request('GET', "http://steamcommunity.com/profiles/{$communityId}/inventory/json/730/2");

    $inventory = [];
    foreach (json_decode($response->getBody()->getContents(), true)['rgDescriptions'] as $item) {
        $inventory[$item['name']] = $item;
    }

    return $inventory;
}

我刚才遇到了同样的问题。问题是 json 代码中的随机数,如 00000000 和 00006000。这看起来是 Guzzle 中的一个错误。我能够通过安装 php-curl 包来修复它。尝试像这样执行 apt-get 来解决问题(如果你的 OS 像 Ubuntu):

sudo apt-get install php-curl

您可以在这里找到更多相关信息: https://github.com/guzzle/guzzle/issues/1385