【问题标题】:GET info from external Array/API/URL using PHP使用 PHP 从外部 Array/API/URL 获取信息
【发布时间】:2015-04-18 08:08:22
【问题描述】:

我有一个指向数组的 url http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132

我想获取第一个'sellorders'的值,在这种情况下是:0.00000048并将其存储在变量$sellorderprice中。

谁能帮忙?

谢谢。

【问题讨论】:

  • 尝试使用 json_decode($output);它会将 json 字符串转换为数组格式。

标签: php arrays json


【解决方案1】:

只需通过file_get_contents 访问网址内容。您的页面实际上返回一个 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 很高兴这有帮助
【解决方案2】:

很简单,你只需要像这样解码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'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-13
    • 2018-10-21
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多