【问题标题】:Print JSON data from currencyconverterapi从 currencyconverterapi 打印 JSON 数据
【发布时间】:2018-05-03 04:15:09
【问题描述】:

我正在尝试打印货币转换器 API JSON 数据中的值。

任何人都可以帮助我打印来自该 URL 的值

https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y?

【问题讨论】:

  • Iam Loki 检查下面的答案

标签: php json currency


【解决方案1】:

您必须将file_get_contents()json_decode() 一起使用

<?php

$json_data = file_get_contents('https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y');

$array = json_decode($json_data, true);

var_dump($array["USD_IDR"]["val"]);

?>

我已经在本地机器上测试过并且工作正常:-

https://prnt.sc/jd1kxohttps://prnt.sc/jd1l7w

【讨论】:

  • 感谢@Alive to Die
  • @IamLoki 很高兴为您提供帮助:):)
【解决方案2】:

使用Json_decode

$data = json_decode('{"USD_IDR":{"val":13965}}', TRUE);
var_dump($data["USD_IDR"]["val"]); //int(13965)

【讨论】:

    【解决方案3】:

    试试这个:

      ob_start();
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,'https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $response = curl_exec($ch);
      $jsontoarr = json_decode($response);
    
      echo $jsontoarr->USD_IDR->val;
    

    祝你好运。

    【讨论】:

      猜你喜欢
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      相关资源
      最近更新 更多