douque8861 2013-02-13 20:08
浏览 48
已采纳

使用当前货币转换率动态更新表格

Edit:

Well, I got it working thanks to jimmy's link. Here is the code I'm using to convert just in case someone else wants to try the same thing. This works best with a number you know is basically fixed or doesn't change often. Make sure you remove the notes as denoted by /// so you don't have floating text if you use the code below. Also note, the results are approximate so you might want to add a section that says as much. Liability stuff.

The code required to fetch the data from Google's API:

<?php

function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return round($var,9); ///the 9 indicates how many decimal points you want. I set it at the API's limit of 9
}
?>

<?php ///turns the exchange rate into a php variable so we can use it later. 
$cad = currency("USD","CAD",1) ; ///The currency code converting from, the currency code converting to, and amount you want to convert. 
?> ///Best use 1 so for a simpler equation. (base amount * exchange rate = how much you'll need)

<?php 
$gbp = currency("USD","GBP",1) ; 
?>

The table with content:

<table border="1" style="margin-left: 35px; width:400px;">
<tbody>
<tr>
<td style="text-align:left; width:190px"><strong>Currency</strong></td>
<td style="text-align: center;"><strong>USD</strong></td>
<td style="text-align: center;"><strong>CAD</strong></td>
<td style="text-align: center;"><strong>GBP</strong></td>
</tr>

<tr>
<td style="text-align:left;">Conversion</td>
<td style="text-align: center;">$5000</td> ///the base number I will be using to make the conversion.

<td style="text-align: center;">$<?php 
$basecad = $cad * 5000; ///multiply the base value (in this case $5000) by the conversion ratio stored in our php variable
$basecad = round($basecad, 0); ///rounds it up to 0 decimals for cleanliness
$basecad = number_format($basecad); ///formats it to have commas (1,000 vs 1000)
echo $basecad; ///prints result
?></td>

<td style="text-align: center;">$<?php  ///repeat as necessary for additional currencies or lines
$basegbp = $gbp * 5000;
$basegbp = round($basegbp, 0);
$basegbp = number_format($basegbp);
echo $basegbp;
?></span></td>

</tr>
</tbody>
</table>

Original Question

So, I had a strange and interesting idea for a company website I'm updating. The page details include currency exchange rates between a few different countries to help with trip planning. The idea I came up with is to update the exchange rates based on whatever the rate is when the page loads. The concept itself is similar to something I've done before but this time I have to use an iframe as generated by a third party. The iframe itself is basically just a currency table from http://themoneyconverter.com/WebTools.aspx. It outputs information into a table and each includes an id for whatever currency rate it is. My question now is how can I extract the information from the iframe's and turn it into a PHP variable I can use in the parent page. After that it's simple PHP calculations to output each expense result. The main problem comes from the fact I cannot modify the information in the iframe. I've delved the internet but cannot find anything but how to put PHP variables into an iframe, not take it out. Below is a sample of the iframe, namely the part I want to extract. Any help, either yes it's possible or no it's not, would be greatly appreciated.

The actual table I'm extracting it into is just an HTML table. I'll just be adding in a basic algorithm to convert the currency (i.e. $var1 x $iframecvar1) for each cell. If you need more info let me know and thanks ahead of time.

<table summary="Exchange Rates for Japanese Yen" id="major-currency-table">...
<td><div class="tmc i-USD"> </div></td> 
<td><a target="_blank" ,="" title="United States Dollar" href="/USD/Exchange_Rates_For_US_Dollar.aspx">USD</a></td> 
<td id="USD">0.01070</td>
  • 写回答

2条回答 默认 最新

  • donglie1994 2013-02-13 20:45
    关注

    Without knowing how much you can alter the page you are working with or what that page looks like, I'd like to suggest an alternative to fetching the exchange rates from an iframe.

    This script works quite well.

    <?php
    function currency($from_Currency,$to_Currency,$amount) {
    $amount = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency = urlencode($to_Currency);
    $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    $data = explode('"', $rawdata);
    $data = explode(' ', $data['3']);
    $var = $data['0'];
    return round($var,3);
    }
    
    echo currency("GBP","USD",1); 
    ?>
    

    If this doesn't answer your problem, maybe it's of use to someone else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误