我正在尝试使用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>";
}
?>
答案 0 :(得分:0)
它是空白的,因为您将数据发布到新页面。
在 index.php 中,您有
行<div id='output'></div>
这是您期望输出结束的地方。但是当你在表单上推送提交时,你实际上会去一个完全不同的页面。如果您查看网址,它现在以 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>
答案 1 :(得分:0)
您需要传递以下功能中的参数,该功能会点击Google网址并获取货币转换后的响应。
参数:
$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(按今天的美元汇率计算)
答案 2 :(得分:0)
Google已更新该链接,即已将其移至新网站。所以这可能会有所帮助:
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转换为尼泊尔卢比。
类似可以用于美元兑换英镑。
答案 3 :(得分:0)
恕我直言,它可以更好地通过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"));
答案 4 :(得分:0)
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;
}