php Google Currency Converter 已更改其 URL 但未获得相同结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19838049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Currency Converter has changed its URL but not getting same result
提问by Simon Davies
I have the following code (below) and was using the iGoogle version.
我有以下代码(如下)并且使用的是 iGoogle 版本。
$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];
BUT having looked they are using a different URL:
但是经过查看,他们使用了不同的 URL:
'http://www.google.com/finance/converter?hl=en&a=' . $amount . '&from=' . $from_Currency . '&to=USD';
But simply changing the url does not return the required result that i was used to.
但简单地更改 url 不会返回我习惯的所需结果。
Now all i would get is
现在我会得到的是
http://www.w3.org/TR/html4/strict.dtd
SO has anyone worked on this latest currency converter URL or have any ideas. Or a replacement using PHP
所以有人在这个最新的货币转换器 URL 上工作过或有任何想法。或者使用 PHP 替换
回答by Simon Davies
Thanks to some more in depth looking and rewording of issue found this post. So in a way its a duplicate. but here is the question:
由于对问题进行了更深入的研究和重新措辞,发现了这篇文章。所以在某种程度上它是重复的。但问题是:
Need API for currency converting
I used @hobailey answer for a temporary fix until i can update it to another version or google decide to do a proper api.
我使用@hobailey 回答进行临时修复,直到我可以将其更新到另一个版本或谷歌决定做一个适当的 api。
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$get = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency");
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);
$converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);
回答by ABC
Using XPath:
使用 XPath:
function currency($from, $to, $amount)
{
$content = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to);
$doc = new DOMDocument;
@$doc->loadHTML($content);
$xpath = new DOMXpath($doc);
$result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue;
return str_replace(' '.$to, '', $result);
}
echo currency('USD', 'EUR', 1); // returns 0.7216
回答by Ujjwal Ojha
I have created a class to connect to Google in easier way here.
我创建了一个类连接到谷歌在更简单的方法在这里。
I hope it should make some aspects easier!
我希望它能让某些方面变得更容易!
Edit:I just knew that Google service was closed this November 2013.
编辑:我只知道 Google 服务已于 2013 年 11 月关闭。
I am gonna have to change it!
我要改变它!
Edit Again: I have changed Google Api to Yahoo Api and it works perfectly fine!
再次编辑:我已将 Google Api 更改为 Yahoo Api,并且运行良好!
回答by PlanetHackers
I have found another solution. It will also work if your server IP is not able to use google service.
我找到了另一个解决方案。如果您的服务器 IP 无法使用 google 服务,它也将起作用。
<?php
$from_currency = 'USD';
$to_currency = 'INR';
$amount = 1;
$results = converCurrency($from_currency,$to_currency,$amount);
$regularExpression = '#\<span class=bld\>(.+?)\<\/span\>#s';
preg_match($regularExpression, $results, $finalData);
echo $finalData[0];
function converCurrency($from,$to,$amount){
$url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to";
$request = curl_init();
$timeOut = 0;
curl_setopt ($request, CURLOPT_URL, $url);
curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
$response = curl_exec($request);
curl_close($request);
return $response;
}
?>
The source reference Step Blogging
源码参考Step Blogging