我想获取过去1个月的汇率数据,我正在使用fastforex.io API的数据。数据没有显示在表中。有人能帮助我吗?
// Function to generate the exchange rate table
function exchange_rate_table_shortcode() {
// Set the API endpoint URL
$url = "https://api.fastforex.io/time-series?from=AED&to=USD&start=2022-05-11&end=2022-06-11&api_key=xxxxxxxx"; // api key on purpose
// Send the request to the API endpoint
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Decode the response
$data = json_decode($response, true);
// Start building the table HTML
$table = "<table>";
$table .= "<tr><th>Date</th><th>Exchange Rate</th></tr>";
// Loop through the data and add a row to the table for each date
foreach ($data as $date => $rate) {
$table .= "<tr><td>" . $date . "</td><td>" . $rate . "</td></tr>";
}
// Finish building the table HTML
$table .= "</table>";
// Return the table HTML
return $table;
}
// Register the shortcode
add_shortcode("exchange_rate_table", "exchange_rate_table_shortcode");
这是API的响应
{
"start": "2022-05-11",
"end": "2022-06-11",
"interval": "P1D",
"base": "AED",
"results": {
"USD": {
"2022-05-11": 0.27236,
"2022-05-12": 0.27233,
"2022-05-13": 0.27236,
"2022-05-14": 0.27232,
"2022-05-15": 0.27232,
"2022-05-16": 0.27233,
}
},
"ms": 7
}
2条答案
按热度按时间nkkqxpd91#
基本上,
date
和rate
数据位于USD
元素的results
中。因此,您的
foreach
循环代码如下所示:nlejzf6q2#
使用静态json构建它:
差异:
您试图将数组显示为值,因此可能会得到以下内容:
Array to string conversion
错误。为什么这段代码很有帮助?因为如果在明天的情况下,结果包括多个键:"USD"、"CAD"等。因此,此代码可能对用例有帮助。