2
Google 통화 변환 API를 사용하여 사용자가 자바 스크립트 기반 드롭 다운 목록에서 선택한 통화를 변환하는 웹 사이트를 만들었습니다. Google 통화 변환기 API 및 자바 스크립트 드롭 다운 목록에서 File_get_contents 오류가 발생했습니다.
나는 다음과 같은 오류가 양식을 제출 한 후 :file_get_contents(12gbp=?usd) [function.file-get-contents]: failed to open stream:
이 즉시 URL이 잘못 나에게 제안합니다. 다음은
: 구글의 API에 위의 인수를 복사 할 경우 직접 http://www.google.com/ig/calculator?h1=en&q=12gbp=?usd
나는 내가 기대하고있어 결과를 받기 때문에 이런 경우가 생각하지 않습니다 관련 코드 :
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>
는 클라이언트 측 하나와 서버 측 언어를 결합하는 시도에서 경험이 문제인가?
미리 감사드립니다.
로컬 웹 서버에 복사하여 예상대로 작동합니다. –
@EugenRieck 그것은 접근과 관련이 있습니다. getCurrencyRate.php와 currency.php 스크립트를 하나의 스크립트에 결합하면 제대로 작동합니다! – keenProgrammer
오류에 따르면'$ googleCurrencyApi'는'currency_convert' 함수에서 비어 있습니다. –