wordpress 数据未显示在表中

50few1ms  于 2023-01-04  发布在  WordPress
关注(0)|答案(2)|浏览(197)

我想获取过去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
}
nkkqxpd9

nkkqxpd91#

基本上,daterate数据位于USD元素的results中。
因此,您的foreach循环代码如下所示:

// Loop through the data and add a row to the table for each date
  foreach ($data['results']['USD'] as $date => $rate) {
    $table .= "<tr><td>" . $date . "</td><td>" . $rate . "</td></tr>";
  }
nlejzf6q

nlejzf6q2#

使用静态json构建它:

$json = '{
   "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-17":0.27233
      }
   },
   "ms":7
}';

        $data = json_decode($json, true);

        // Start building the table HTML
        $table = "<table>";
        $table .= "<tr><th>Date</th><th>Exchange Rate</th></tr>";

        $rates = $data['results'];

        //Assuming that the currency rate could be anything or there could be multiple currency rates as well.
        // Loop through the data and add a row to the table for each date
        foreach ($rates as $currency => $dates) {
            foreach ($dates as $key => $value) {
                $table .= "<tr><td>" . $key . "</td><td>" . $value . "</td></tr>";

            }
            //$table .= "<tr><td>" . $date . "</td><td>" . $rate . "</td></tr>";
        }

// Finish building the table HTML
        $table .= "</table>";

        return $table;

差异:
您试图将数组显示为值,因此可能会得到以下内容:Array to string conversion错误。
为什么这段代码很有帮助?因为如果在明天的情况下,结果包括多个键:"USD"、"CAD"等。因此,此代码可能对用例有帮助。

相关问题