[英]Google Currency Converter has changed its URL but not getting same result
我有以下代碼(如下),並使用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];
但看了他們正在使用不同的URL:
'http://www.google.com/finance/converter?hl=en&a=' . $amount . '&from=' . $from_Currency . '&to=USD';
但只是更改網址並不會返回我習慣的所需結果。
現在我得到的只是
http://www.w3.org/TR/html4/strict.dtd
SO有任何人都在研究這個最新的貨幣轉換器URL或有任何想法。 或使用PHP替換
感謝一些更深入的查找和重寫問題發現這篇文章。 所以在某種程度上它是重復的。 但問題是:
我使用@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]);
使用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
我找到了另一個解決方案。 如果您的服務器IP無法使用谷歌服務,它也會工作。
<?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;
}
?>
源參考步驟博客
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.