使用PHP从外部数组/API/URL获取信息

4

尝试使用json_decode($output)函数,它可以将一个JSON字符串转换为数组格式。 - Nagama Inamdar
2个回答

2

只需通过file_get_contents访问url内容。您的页面实际上返回一个JSON字符串,为了将这些值解码为有意义的数据,请使用json_decode进行解码,然后相应地访问所需的数据:

$url = 'http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132';
$data = json_decode(file_get_contents($url), true);
$sellorderprice = $data['return']['DOGE']['sellorders'][0]['price'];
echo $sellorderprice;

这段代码实际上直接指向索引零0,该索引获取第一个价格。如果需要获取所有物品并只需将它们全部输出,您需要通过foreach迭代所有物品:

foreach($data['return']['DOGE']['sellorders'] as $sellorders) {
    echo $sellorders['price'], '<br/>';
}

当然,@Ben,很高兴这有所帮助。 - Kevin

1

这很简单,您只需像这样解码json:

    $json = file_get_contents("http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132");
    $arr = json_decode($json, true);
    $sellorderprice = $arr['return']['DOGE']['sellorders'][0]['price'];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接