我正在尝试使用PHP中的Google API制作货币转换器,但它给了我一个空白页。USD to GBP到PHP的货币转换器
的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>";
}
?>
问题是什么? – 2014-09-20 15:40:23
...我如何使它工作? – James 2014-09-20 15:43:21
它不起作用?你期望什么,你会得到什么? – 2014-09-20 16:15:39