【问题标题】:Update table dynamically with current currency conversion rates使用当前货币兑换率动态更新表格
【发布时间】:2013-02-13 20:08:56
【问题描述】:

编辑

好吧,多亏了 jimmy 的链接,我才开始工作。这是我用来转换的代码,以防其他人想尝试同样的事情。这最适用于您知道基本固定或不经常更改的数字。确保删除由 /// 表示的注释,以便在使用下面的代码时没有浮动文本。另请注意,结果是近似的,因此您可能需要添加一个说明相同内容的部分。责任的东西。

从 Google 的 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) ; 
?>

包含内容的表格:

<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>

原始问题

所以,我对正在更新的公司网站有了一个奇怪而有趣的想法。页面详细信息包括几个不同国家之间的货币汇率,以帮助制定旅行计划。我想出的想法是根据页面加载时的汇率更新汇率。这个概念本身与我之前做过的类似,但这次我必须使用第三方生成的 iframe。 iframe 本身基本上只是来自http://themoneyconverter.com/WebTools.aspx 的货币表。它将信息输出到一个表格中,每个表格都包含一个用于任何货币汇率的 id。我现在的问题是如何从 iframe 中提取信息并将其转换为我可以在父页面中使用的 PHP 变量。之后是简单的 PHP 计算来输出每个费用结果。主要问题来自我无法修改 iframe 中的信息这一事实。我已经深入研究了互联网,但除了如何将 PHP 变量放入 iframe 中,找不到任何东西,而不是将其取出。下面是 iframe 的示例,即我要提取的部分。任何帮助,无论是可能的还是不可能的,都将不胜感激。

我将其提取到的实际表格只是一个 HTML 表格。我将添加一个基本算法来转换每个单元格的货币(即 $var1 x $iframecvar1)。如果您需要更多信息,请提前告诉我并感谢。

<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>

【问题讨论】:

    标签: php html-table


    【解决方案1】:

    在不知道您可以对正在使用的页面进行多少更改或该页面的外观的情况下,我想建议一种替代方法,而不是从 iframe 中获取汇率。

    This script 效果很好。

    <?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); 
    ?>
    

    如果这不能解决您的问题,也许它对其他人有用。

    【讨论】:

    • 你解决了我的问题。这允许我从页面内的汇率而不是 iframe 中创建变量,这意味着我可以在一个页面上创建所有内容。这也很简单。再次感谢您的好先生。
    【解决方案2】:

    在您的特定情况下,iframe 正在从 Google 加载 jQuery,并包含来自 themoneyconverter.com 的 Google Analytics - 因此在您自己的页面上简单地回显它可能会导致不希望的结果。

    也就是说,网页抓取可能会很棘手,但这里有一个简单的工作流程 - 在 PHP 中获取数据并使用 file_get_contents http://php.net/manual/en/function.file-get-contents.php 转换为 var

    <?php
    $file = file_get_contents('http://themoneyconverter.com/USD/RateTicker.aspx');
    //echo the file
    echo $file;
    

    如果你想更复杂地抓取内容,我建议 Matthew Turland 的博客或他关于该主题的书:

    http://matthewturland.com/tag/web-scraping/

    【讨论】:

    • 嗯,我真的把它当成一个隐藏的 iframe,不需要直接拉,除非网页抓取是唯一的方法。所以,如果我只是在 iframe 中有一个静态表,我无法使用 PHP 将其拉入父页面?
    • 我认为您不太了解 Web 请求的工作原理,以及 iframe 实际上是什么:)
    • 我承认我不是任何编码方面的专家,但我不喜欢居高临下的陈述。如果您有一些有用的东西要添加,请做。如果不是那么请不要评论。我知道 iframe 是一个独立的窗口,只是放置在父页面中并自行运行。在使用 javascript 之前,我已经能够将信息从父页面传递到 iframe,因此为什么我要问其他方法是否有效,以及我是否可以在 PHP 中做到这一点。
    • 谢谢你,菲尔,在对抓取进行了更多研究之后,我明白你为什么推荐它了。我现在还注意到,出于安全原因,不能从中提取使用不同 DOM 的 iFrame。实际上,由于跨站点安全性,我无法取出元素,而我的 ATS 项目位于允许我传递信息的同一个 DOM 下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2013-08-05
    • 2012-06-10
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多