我需要能够使用 PHP 从字符串中获取一些数据。我从游戏网站上得到了一个API,需要对其进行分解。我需要分解的字符串是这样的: http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513
我需要从该字符串中获取小图标图像,这是字符串中的第一个 url,以及字符串中的当前价格为 887。因此,它指出了这一点:
"当前":{"趋势":"中性","价格":887}
我需要抓住 887 并将其放入变量中。
我正在使用PHP,如果有人可以帮助:),请提前感谢
为此使用 json_decode()
:
$item = json_decode($json)->item;
$price = $item->current->price;
$icon = $item->icon;
下载 JSON 并转换为对象。
$item = json_decode(file_get_contents('http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513'))->item;
$name = $item->name;
// etc.
这是一个您应该随身携带的功能。 它对于打印格式化的对象和数组很有用,这使得检查其结构变得更加容易。
function debug($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}