gpt4 book ai didi

php - Google 货币转换器 API 和 JavaScript 下拉列表出现 File_get_contents 错误

转载 作者:行者123 更新时间:2023-11-30 06:43:10 25 4
gpt4 key购买 nike

我创建了一个网站,该网站使用 Google 的货币转换 API 来转换用户从由 JavaScript 提供支持的下拉列表中选择的货币。

enter image description here

提交表单后出现以下错误:

file_get_contents(12gbp=?usd) [function.file-get-contents]: failed to open stream: 

这立即向我表明 URL 是错误的。我不认为是这种情况,因为如果将上述参数直接复制到 Google 的 API 中

http://www.google.com/ig/calculator?h1=en&q=12gbp=?usd

我收到了我期望的结果:

enter image description here

相关代码如下:

getCurrencyRates.php

<?php

// Feed URL's //
$googleCurrencyApi = 'http://www.google.com/ig/calculator?h1=en&q=';

function currency_convert($googleCurrencyApi, $amount, $master, $slave) {

$result = file_get_contents($googleCurrencyApi . $amount . $master . '=?' . $slave);
$result = str_replace("\xA0", ",", $result);
$expl = explode('"', $result);

if ($expl[1] == '' || $expl[3] == '') {
throw new Exception('An error has occured. Unable to get file contents');
} else {
return array(
$expl[1],
$expl[3]
);
}
}
?>

currency.php

<?php

require 'includes/getCurrencyRates.php';

// Check to ensure that form has been submitted
if (isset($_POST['amount'], $_POST['master'], $_POST['slave'])) {
$amount = trim($_POST['amount']);
$master= $_POST['master'];
$slave = $_POST['slave'];

// Check amount is not empty
if (!empty($amount)) {
// Check amount is higher than 1 inclusive
if ($amount >= 1) {
try {
$conversion = currency_convert($googleCurrencyApi, $amount, $master, $slave);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage();
}

// Check URL has been formed
if ($conversion == false) {
echo 'Sorry, something went wrong';
} else {
echo $conversion[0], ' = ', $conversion[1];
if ($from == $to) {
echo '<p>That was a pointless conversion!</p>';
}
}
} else {
echo 'Number too small. You must enter a number higher than 1 inclusive';
}
} else {
echo 'You must enter a number into amount';
}
}
?>

<body onload="changeList(document.forms['drops'].master)">

<script language="javascript">
var lists = new Array();

// First set of text and values
lists['gbp'] = new Array();
lists['gbp'][0] = new Array(
'usd',
'eur'
);
lists['gbp'][1] = new Array(
'usd',
'eur'
);


// Second set of text and values
lists['usd'] = new Array();
lists['usd'][0] = new Array(
'gbp',
'eur'
);
lists['usd'][1] = new Array(
'gbp',
'eur'
);

// Third set of text and values
lists['eur'] = new Array();
lists['eur'][0] = new Array(
'gbp',
'usd'
);
lists['eur'][1] = new Array(
'gbp',
'usd'
);
</script>

<script language="javascript">

// This function goes through the options for the given
// drop down box and removes them in preparation for
// a new set of values

function emptyList( box ) {
// Set each option to null thus removing it
while ( box.options.length ) box.options[0] = null;
}

// This function assigns new drop down options to the given
// drop down box from the list of lists specified

function fillList( box, arr ) {
// arr[0] holds the display text
// arr[1] are the values

for ( i = 0; i < arr[0].length; i++ ) {

// Create a new drop down option with the
// display text and value from arr

option = new Option( arr[0][i], arr[1][i] );

// Add to the end of the existing options

box.options[box.length] = option;
}

// Preselect option 0

box.selectedIndex=0;
}

// This function performs a drop down list option change by first
// emptying the existing option list and then assigning a new set

function changeList( box ) {
// Isolate the appropriate list by using the value
// of the currently selected option

list = lists[box.options[box.selectedIndex].value];

// Next empty the slave list

emptyList( box.form.slave );

// Then assign the new list values

fillList( box.form.slave, list );
}
</script>

<form name="drops" method="post" action="">
<table border=0 bgcolor="#ffeecc">
<tr>
<td>Amount e.g. 10</td>
<td><input type="text" name="amount" /></td>
</tr>
<tr>
<td>Select from</td>
<td><select name="master" size=1 onchange="changeList(this)">
<option value="gbp">gbp
<option value="usd">usd
<option value="eur">eur
</select>
</td>
</tr>
<tr>
<td>Select to</td>
<td>
<select name="slave" size=1>
<option>Netscape 4 width
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Convert Now" />
</td>
</tr>
</table>
</form>

这个问题是在尝试将服务器端语言与客户端语言结合时遇到的吗?

提前致谢。

最佳答案

Shiplu 是对的。可能与variable scope有关,这真的没有多大意义。至少谷歌不是问题。

关于php - Google 货币转换器 API 和 JavaScript 下拉列表出现 File_get_contents 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com