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

发布于 2025-01-05 07:39:24 字数 6255 浏览 0 评论 0原文

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

在此处输入图像描述

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

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

我收到了我期望的结果:

在此处输入图像描述

这是相关代码:

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>

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

提前致谢。

I've created a website that uses Google's Currency Convert API to convert currencies that a user selects from a drop down list powered by JavaScript.

enter image description here

After submitting the form I get the following error:

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

This immediately suggests to me that the URL is wrong. I don't feel that this is the case because if copy the above arguments into Google's API directly

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

I receive the result that I'm expecting:

enter image description here

Here is the relevant code:

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>

Is this problem experienced from trying to combine a server side language with a client side one?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愚人国度 2025-01-12 07:39:24

希普鲁是对的。它可能与变量作用域有关,实际上并没有关系很有意义。至少对于谷歌来说这不是问题。

Shiplu is right. It may have something to do with variable scope, which really doesn't make much sense. It is not a problem with google at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文