当前位置: 动力学知识库 > 问答 > 编程问答 >

GET info from external Array/API/URL using PHP

问题描述:

I have the url http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=132 which leads to an array.

I want to get the value of the first 'sellorders' which in this case is: 0.00000048 and store it in the variable $sellorderprice.

Can anyone help?

Thanks.

网友答案:

Just access the url contents thru file_get_contents. Your page actually return a JSON string, to get those values into meaningful data, decode it thru json_decode, after that access the data needed accordingly:

$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;

That code actually points directly to index zero 0 which gets the first price. If you need to get all items an just simply echo them all you need to iterate all items thru foreach:

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

Its simple, you just have to decode json like this:

    $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'];
分享给朋友:
您可能感兴趣的文章:
随机阅读: