无法找出转换字符串

时间:2018-07-09 22:39:04

标签: php

有人可以帮忙吗?我正在尝试获取INR价格,但这只能给我USD

(字符串)转换-以另一种货币形式返回定价信息。我不知道该怎么办。

<?php  
error_reporting(0);
$url = 'https://api.coinmarketcap.com/v2/ticker/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result1 = curl_exec($ch);
curl_close($ch);
$live_price = json_decode($result1, true);
?>

2 个答案:

答案 0 :(得分:0)

请求:https://api.coinmarketcap.com/v2/ticker/提供以下JSON:

"data": {
    "1": {
        "id": 1, 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "website_slug": "bitcoin", 
        "rank": 1, 
        "circulating_supply": 17140537.0, 
        "total_supply": 17140537.0, 
        "max_supply": 21000000.0, 
        "quotes": {
            "USD": {
                "price": 6780.98, 
                "volume_24h": 3704860000.0, 
                "market_cap": 116229638586.0, 
                "percent_change_1h": -0.42, 
                "percent_change_24h": -0.03, 
                "percent_change_7d": 2.62
            }
        }, 
        "last_updated": 1531177530
    }, 
    etc...

意思是,遍历所有值:

foreach($live_price['data'] as $mainkey => $index){
  foreach($index as $k => $v){
    if($k == 'quotes'){
      $v = $live_price['data'][$mainkey]['quotes']['USD'];
      echo $v['price'];
    } else {
      echo "Hi, I'm index as '$k' and my value is '$v'";
    }
  }
}

答案 1 :(得分:0)

将$ url更改为此: $url = 'https://api.coinmarketcap.com/v2/ticker/?convert=INR';

它将同时给您'USD'和'INR'。