我正试图在PHP中使用Google API制作一个货币转换器,但它给了我一个空白页面。
index.php:
<div id='output'></div>
<form action='convert.php' method='post'>
Amount: <input name='amount' type='text'><br/>
From: <select name='from'>
<option value='USD'>USD</option>
<option value='GBP'>GBP</option>
</select><br/>
To: <select name='to'>
<option value='GBP'>GBP</option>
<option value='USD'>USD</option>
</select><br/>
<input name='submit' type='submit' value='submit'>
</form>
convert.php:
<?php
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);
}
if(!empty($_POST) && isset($_POST['submit'])) {
$output = currency($_POST['from'], $_POST['to'], $_POST['amount']);
echo "<script>document.getElementById('output').innerHTML = '<p>" . $output . "</p>'></script>";
}
?>
它是空白的,因为您正在将数据发布到新页面。
在index.php中,您有一行
<div id='output'></div>
这就是您期望输出结束的地方。但当你在表单上推送提交时,你实际上会进入一个完全不同的页面。如果您查看url,它现在以convert.php结束
如果你点击提交并查看页面的来源,它实际上包含了你告诉它的内容
只需将output.php中的第14行替换为:
echo $output;
到时你会看到结果的。
由于您希望输出显示在与表单所在的页面相同的页面上,因此您可能需要研究使用AJAX。
或者,您可以通过以下操作将获得结果的逻辑放在同一页中:
<?php
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);
}
if(!empty($_POST) && isset($_POST['submit'])) {
$output = currency($_POST['from'], $_POST['to'], $_POST['amount']);
}
?>
<div id='output'><?php if(isset($output)) { echo $output; } ?></div>
<form action='index.php' method='post'>
Amount: <input name='amount' type='text'><br/>
From: <select name='from'>
<option value='USD'>USD</option>
<option value='GBP'>GBP</option>
</select><br/>
To: <select name='to'>
<option value='GBP'>GBP</option>
<option value='USD'>USD</option>
</select><br/>
<input name='submit' type='submit' value='submit'>
</form>
您需要在下面的函数中传递参数,该函数会命中Google URL并获取货币转换后的响应。
参数:$from_Currency
=您想要的响应$to_Currency
=您需要从中转换$amount
=要转换的金额。
<?php
function get_currency($from_Currency, $to_Currency, $amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$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('bld>', $rawdata);
$data = explode($to_Currency, $data[1]);
return round($data[0], 2);
}
// Call the function to get the currency converted
echo get_currency('USD', 'INR', 1);
?>
响应:
65.24(按今天的美元汇率)
谷歌已经更新了链接,即它已被移动到一个新网站。因此,这可能会有所帮助:
function currencyConvert($from,$to,$amount){
$url = "https://finance.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);
$rawdata = str_replace('</span>','',str_replace('<span class="bld">','',$response));
list(,$val) = explode("bld>",$rawdata);
list($raw2,) = explode("<input",$val);
return (float) $raw2;
}
调用此函数:
echo currencyConvert('USD','NPR',1);
这将把1美元兑换成尼泊尔卢比。
美元对英镑也可以使用类似的货币。
IMHO最好通过API进行转换,因为加载所有页面可能很慢且不可破坏。
function convertCurrency($amount, $from, $to){
$conv_id = "{$from}_{$to}";
$string = file_get_contents("http://free.currencyconverterapi.com/api/v5/convert?q=$conv_id&compact=ultra");
$json_a = json_decode($string, true);
return $amount * round($json_a[$conv_id], 4);
}
//EXAMPLE convert 29 euro in $
echo(convertCurrency(29, "EUR", "USD"));
finance.google.com已停产,尝试这些:
// google API - Load time: 558 ms
function google_money_convert($from, $to, $amount)
{
$url = "https://www.google.com/search?q=".$from.$to;
$request = curl_init();
$timeOut = 0;
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
curl_setopt($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
$response = curl_exec($request);
curl_close($request);
preg_match('~<span [^>]* id="knowledge-currency__tgt-amount"[^>]*>(.*?)</span>~si', $response, $finalData);
$finalData=str_replace(',', '.', $finalData);
return (float)$finalData[1]*$amount;
}
// free.currencyconverter API - Load time: 95ms
function money_convert($from, $to, $amount)
{
$url = "http://free.currencyconverterapi.com/api/v5/convert?q=$query&compact=ultra";
$request = curl_init();
$timeOut = 0;
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
curl_setopt($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
$response = curl_exec($request);
curl_close($request);
$response = json_decode($response, true);
$responseOld=$response;
// print_r($response);
return $response[$query]*$amount;
}