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



我有指向数组的网址 http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132。

我想获取第一个' sellorders'的值,在本例中为: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/>';
}

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

   $json = file_get_contents("http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132");    $arr = json_decode($json, true);    $sellorderprice = $arr['退货']['DOGE']['卖单'][0]['价格'];

最新更新