我正在使用" new"谷歌货币计算器,而不是先前的版本,现在我们有一个表格,下拉菜单..等等,我需要的只是获得结果值。
这是我简单的php函数:
// GOOGLE CURRENCY CONVERTER API
function converter_currency($amount, $from){
$result = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to=USD');
print_r ($result);
// echo gettype($result); <== said that it's a string
}
converter_currency(5, 'EUR'); //converts 5euros to USD
返回结果的表单.. 5 EUR = 5.4050 USD
如何获得:5.4050
?
谢谢。
答案 0 :(得分:3)
function converter_currency($amount, $from, $to="USD"){
$result = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to);
if($doc = @DomDocument::loadhtml($result)){
if($xpath=new DomXpath($doc)){
if($node = $xpath->query("//span")->item(0)){
$result = $node->nodeValue;
if($pos = strpos($result," ")){
return substr($result,0,$pos);
}
}
}
}
return "Error occured";
}
答案 1 :(得分:1)
function converter_currency($amount, $from){
$result = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to=USD');
preg_match('#\<span class=bld\>(.+?)\<\/span\>#s', $result, $finalData);
return $finalData[1];
}
echo converter_currency(5, 'EUR'); // 5.4125 USD
list($amount, $currency) = explode(' ', converter_currency(5, 'EUR'));
var_dump($amount, $currency); // string(6) "5.4125" string(3) "USD"