Php 使用当前货币兑换率动态更新表

Php 使用当前货币兑换率动态更新表,php,html-table,Php,Html Table,编辑: 好吧,多亏了吉米的链接,我才成功。下面是我用来转换的代码,以防其他人想尝试同样的事情。如果你知道一个数字基本上是固定的,或者不经常变化,那么这个方法最有效。确保删除了///表示的注释,这样如果使用下面的代码,就不会有浮动文本。另外请注意,结果是近似的,因此您可能希望添加一个部分,说明同样多的内容。责任的东西 从谷歌API获取数据所需的代码: <?php function currency($from_Currency,$to_Currency,$amount) { $amount

编辑

好吧,多亏了吉米的链接,我才成功。下面是我用来转换的代码,以防其他人想尝试同样的事情。如果你知道一个数字基本上是固定的,或者不经常变化,那么这个方法最有效。确保删除了///表示的注释,这样如果使用下面的代码,就不会有浮动文本。另外请注意,结果是近似的,因此您可能希望添加一个部分,说明同样多的内容。责任的东西

从谷歌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) ; 
?>

在您的特定情况下,iframe正在从Google加载jQuery,并包含来自MoneyConverter.com的Google分析-因此在您自己的页面上简单地重复此内容可能会导致不希望的结果

也就是说,web抓取可能会变得很棘手,但这里有一个简单的工作流程——用PHP获取数据,并将其转换为具有
file\u get\u contents


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

效果很好

<?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,不需要直接拉,除非拉网是唯一的方法。所以,如果我只是在iframe中有一个表,静态的,我无法使用PHP将其拉入父页面?我认为您不太了解web请求的工作方式,以及iframe实际上是什么:)我承认我在任何类型的编码方面都不是专家,但我不喜欢居高临下的陈述。如果你有什么有用的补充,请做。如果没有,请不要评论。我知道iframe是一个独立的窗口,它只放置在父页面中,并独立运行。我以前使用javascript能够将信息从父页面传递到iframe,因此我想问一下,为什么用另一种方法可以,以及我是否可以用PHP来实现。谢谢phil,在对scraping进行了更多的研究之后,我明白了你为什么推荐它了。我现在还注意到,出于安全原因,无法从中提取使用不同DOM的IFrame。实际上,由于跨站点的安全性,我无法取出元素,而我的ATS项目位于同一个DOM下,该DOM允许我传递信息。您已经解决了我的问题。这使我能够在页面内,而不是在iframe中,根据汇率生成变量,这意味着我可以在一个页面上创建所有内容。它也很简单。再次谢谢你,好先生。
<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
$file = file_get_contents('http://themoneyconverter.com/USD/RateTicker.aspx');
//echo the file
echo $file;
<?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); 
?>