从网站获取Json数据返回“string(0)”

时间:2016-03-08 20:39:07

标签: php json

我试图使用以下代码从网站获取数据:

<?php
$url = 'http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=4798';
$content = file_get_contents($url);
var_dump($content);
$json = json_decode($content, true);

var_dump($json);

for ($idx = 0; $idx < count($json); $idx++) {
$obj = (Array)$json[$idx];
echo 'result' . $obj["name"];
}
?>

这对我有这样的结果: http://take.ms/4PzZM

2 个答案:

答案 0 :(得分:1)

<?php
$url = 'http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=4798';
$content = file_get_contents($url);
echo "<pre>";
//print_r($content);
$data = json_decode($content);
print_r($data);  //Show the json decoded data comes form $url 

##Parse this array {$data} using foreach loop as your use  
?>

答案 1 :(得分:0)

您在问题中发布的网址返回的json中没有数字键。因此,使用数字键迭代关联数组不会返回任何内容。

这是你正在使用的json的结构:

IntVar

尝试访问{ "item": { "icon": "http://services.runescape.com/m=itemdb_oldschool/5122_obj_sprite.gif?id=4798", "icon_large": "http://services.runescape.com/m=itemdb_oldschool/5122_obj_big.gif?id=4798", "id": 4798, "type": "Default", "typeIcon": "http://www.runescape.com/img/categories/Default", "name": "Adamant brutal", "description": "Blunt adamantite arrow... ouch.", "current": { "trend": "neutral", "price": 529 }, "today": { "trend": "neutral", "price": 0 }, "members": "true", "day30": { "trend": "negative", "change": "-9.0%" }, "day90": { "trend": "negative", "change": "-20.0%" }, "day180": { "trend": "negative", "change": "-31.0%" } } } 。这应该会给你一些更有意义的东西。如果要迭代项中的键/值对,请使用$json["item"]循环:

foreach