gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:05:12 30 4
gpt4 key购买 nike

我正在使用 currencylayer JSON API 获取实时货币转换值 - 有谁知道我如何从中获取 "result" 值和 "quote" 值下面使用 PHP 的 API 响应?

我是 PHP 的新手,我想知道是否可以将它存储在一个变量中。

这是 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
}

我玩过 file_get_contents("URL") 但我不明白如何获取单个值。

请求 URL 如下所示:

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

感谢您的帮助!

最佳答案

好吧,假设 json 响应在一个名为 $response 的变量中,您必须使用 json_decode 然后执行以下操作:

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

关于PHP 从 API 响应中获取 json 汇率值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30513525/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com