使用Google货币转换器API和JavaScript下拉列表时出现File_get_contents错误

时间:2012-02-12 13:33:46

标签: php javascript file-get-contents

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

enter image description here

提交表单后,我收到以下错误:

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

这立即告诉我网址错误。我不认为是这种情况,因为如果直接将上述论点复制到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>

尝试将服务器端语言与客户端语言相结合是否会遇到此问题?

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

相关问题