【问题标题】:Parsing JSON in PHP is not giving response在 PHP 中解析 JSON 没有给出响应
【发布时间】:2016-02-19 11:06:43
【问题描述】:

所以我试图从 JSON 文件中获取一些信息。但由于某种原因,我需要的信息没有实际响应。

这是我正在运行的代码。

$json_object = file_get_contents('http://steamcommunity.com/profiles/'.$steamID.'/inventory/json/730/2');
$json_decoded = json_decode($json_object); 
echo $json_decoded->success;

我从 cookie 中获取 steamID。但是,如果您想查看 JSON 的外观,则可以查看此链接: http://steamcommunity.com/profiles/76561198031313244/inventory/json/730/2

【问题讨论】:

  • 你在$json_object里面做什么?
  • 可能不是 utf8 吗? $json_object 中有什么?
  • 你期待什么,它给了我一个很长的 json 响应......并得到echo $json_decoded->success; is 1
  • 您确定$json_object 不为空吗?确保 file_get_contents() 按预期工作
  • 我输入了 steamcommunity.com/id/SANDISS/inventory/json/730/2 链接,但仍然没有任何响应。此外,如果您查看最顶部的链接内部,它应该输出“true”

标签: php json parsing


【解决方案1】:

问题是你正在检查的 URL 被重定向到另一个 URL,file_get_contents() 不支持跟随重定向 AFAIK,所以你最好使用cURL

查看这个答案https://stackoverflow.com/a/4324018/5658508

【讨论】:

    【解决方案2】:

    问题就像其他人告诉你的那样,在这个链接中http://steamcommunity.com/profiles/76561198031313244/inventory/json/730/2

    我只是监控响应,所以当您访问该链接时,您会得到一个302 Moved Temporarily http 响应到新链接http://steamcommunity.com/id/SANDISS/inventory/json/730/2

    如果您的链接不仅仅是一个 Steam id,您可以尝试使用 file_get_contents 获取内容,或者使用curl library

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      相关资源
      最近更新 更多