【问题标题】:How to use PHP to fetch and decode JSON data?如何使用 PHP 获取和解码 JSON 数据?
【发布时间】:2023-04-08 18:15:02
【问题描述】:

如何从 JSON url https://api.coinmarketcap.com/v1/ticker/ethereum/ 获取变量“price_usd”和“price_btc”?我写了一个脚本,但没有任何反应。

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

$usd = $data[0]["price_usd"];
echo $usd;
?>

【问题讨论】:

  • print_r($data);

标签: php json curl decode file-get-contents


【解决方案1】:

你的代码使用了 file_get_contents 两次,看看会是:

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

$usd = $data[0]["price_usd"];
echo $usd;
?>

【讨论】:

    【解决方案2】:

    试试这个

    <?php
    $json = file_get_contents('https://api.coinmarketcap.com/v1/ticker/ethereum/'); 
    $data = json_decode($json);
    var_dump($data[0]->price_usd);
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2013-05-18
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      相关资源
      最近更新 更多