需要帮助在php中使用array / json进行coinmarketcap api

时间:2017-12-13 18:21:44

标签: php arrays json

我试图创建一个使用coinmarketcap api来跟踪当前加密货币价格的简单网站,下面的代码可以运行,但它并不能解释货币互相冲突的时间。

    <?php
    $tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/');
    $url = $tick;
    echo $url;
    $json = file_get_contents($url);
    $data = json_decode($tick, TRUE); //decodes in associative array

    $ethusd = $data[1]['price_usd'];
    echo $ethusd;

    ?>

这是正在解码的json

[
{
    "id": "bitcoin", 
    "name": "Bitcoin", 
    "symbol": "BTC", 
    "rank": "1", 
    "price_usd": "16474.4", 
    "price_btc": "1.0", 
    "24h_volume_usd": "13440600000.0", 
    "market_cap_usd": "275760451140", 
    "available_supply": "16738725.0", 
    "total_supply": "16738725.0", 
    "max_supply": "21000000.0", 
    "percent_change_1h": "-2.32", 
    "percent_change_24h": "-5.68", 
    "percent_change_7d": "24.41", 
    "last_updated": "1513187054"
}, 
{
    "id": "ethereum", 
    "name": "Ethereum", 
    "symbol": "ETH", 
    "rank": "2", 
    "price_usd": "684.996", 
    "price_btc": "0.04138", 
    "24h_volume_usd": "4731760000.0", 
    "market_cap_usd": "65976663404.0", 
    "available_supply": "96316859.0", 
    "total_supply": "96316859.0", 
    "max_supply": null, 
    "percent_change_1h": "-5.62", 
    "percent_change_24h": "12.04", 
    "percent_change_7d": "56.0", 
    "last_updated": "1513187055"
}, 
{
    "id": "bitcoin-cash", 
    "name": "Bitcoin Cash", 
    "symbol": "BCH", 
    "rank": "3", 
    "price_usd": "1594.38", 
    "price_btc": "0.0963151", 
    "24h_volume_usd": "1286400000.0", 
    "market_cap_usd": "26871042768.0", 
    "available_supply": "16853600.0", 
    "total_supply": "16853600.0", 
    "max_supply": "21000000.0", 
    "percent_change_1h": "-3.9", 
    "percent_change_24h": "1.8", 
    "percent_change_7d": "8.88", 
    "last_updated": "1513187076"
}, 
etc.

由于以太是json中第二高的货币,它现在有效,但如果它移动它就不会。那么有没有办法让我做一些像$ data [指定以太的东西] [&#39; price_usd&#39;]呢?对任何PHP无知事先道歉,我对PHP /数组/等知之甚少,并且几天前就开始使用它了。

2 个答案:

答案 0 :(得分:0)

我使用他们的API玩了一下,你可以通过在网址中附加货币的slug来获取1种货币:

示例https://api.coinmarketcap.com/v1/ticker/ethereum/

所以您的端点变为https://api.coinmarketcap.com/v1/ticker/ {slug} /

/!\请注意,此端点仍会向您发送JSON数组,因此您必须使用$ data [0]获取第一个索引。 您的代码变为:

<?php
$slug = 'ethereum'; // find all at https://files.coinmarketcap.com/generated/search/quick_search.json
$tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/'.$slug.'/');
$url = $tick;
echo $url;
$json = file_get_contents($url);
$data = json_decode($tick, TRUE); //decodes in associative array

$ethusd = $data[0]['price_usd'];
echo $ethusd;

?>

这里列出了所有的slu {https://files.coinmarketcap.com/generated/search/quick_search.json

PS:API文档在这里https://coinmarketcap.com/api/但是它没有提到slug端点

答案 1 :(得分:0)

我在此理解的是,您希望仅为price_usd ID获取ethereum,为此您可以使用array_columns将所有ID提取为一个数组,然后搜索'ethereum'指数:

$key_ethereum = array_search('ethereum', array_column($data, 'id'));

// array_column($data, 'id') => ['bitcoin', 'ethereum', 'bitcoin-cash']
// array_search('ethereum', ['bitcoin', 'ethereum', 'bitcoin-cash']); => 1

echo $data[ $key_ethereum ]['price_usd'];

Live Demo

希望这有帮助