简体   繁体   English

PHP从API响应中获取json汇率值

[英]PHP grab json exchange rate value from API response

I am using currencylayer JSON API to get realtime currency conversion value - does anybody know how I could grab both the "result" value and the "quote" value from the API response below using PHP? 我正在使用currencylayer JSON API来获取实时货币转换价值 - 有谁知道如何使用PHP从API响应中获取"result"值和"quote"值?

I am new to PHP, and I am wondering if it's possible to store it in a variable. 我是PHP的新手,我想知道是否可以将它存储在变量中。

This is the JSON: 这是JSON:

{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "query":{
    "from":"CAD",
    "to":"GBP",
    "amount":234
  },
  "info":{
    "timestamp":1432829168,
    "quote":0.524191
  },
  "result":122.660694
}

I have played around with file_get_contents("URL") but I didnt understand how to get a single value out. 我玩过file_get_contents("URL")但我不明白如何获得单个值。

Request URL looks like this: 请求网址如下所示:

https://apilayer.net/api/convert?access_key=...&from=CAD&to=GBP&amount=234

Thanks for the help! 谢谢您的帮助!

Ok, lets say that json response is in a variable named $response , you must use json_decode and then do as follows: 好吧,让我们说json响应在一个名为$response的变量中,你必须使用json_decode然后执行如下操作:

$decoded = json_decode($response);
$result = $decoded->result;
$quote = $decoded->info->quote;
var_dump($result, $quote);

Try this 尝试这个

$jsonArray = file_get_contents($yourUrl);
$jsonObject = json_decode($jsonArray);
echo $jsonObject->result;
echo $jsonObject->info->quote;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM