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

我有url
http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132导致阵列.

我想得到第一个’卖家’的价值,在这种情况下是0.00000048并将其存储在变量$sellorderprice中.

有人可以帮忙吗?

谢谢.

最佳答案 只需通过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/>';
}
点赞