【问题标题】:Taking input in link在链接中输入
【发布时间】:2012-01-16 18:19:58
【问题描述】:

这是一个 api:=>

http://www.google.com/ig/api?weather=[city name]

当我像http://www.google.com/ig/api?weather=dhaka那样手动输入[城市名称]时,效果很好。有没有办法让用户输入城市名称。

<?
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=dhaka'); //Manually,I put 'dhaka' here
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
?>
<html>
    <head>
        <title>Google Weather API</title>
    </head>
    <body>
        <h1><?= print $information[0]->city['data']; ?></h1>
        <h2>Today's weather</h2>
        <div class="weather">       
            <img src="<?= 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
            <span class="condition">
            <?= $current[0]->temp_f['data'] ?>&deg; F,
            <?= $current[0]->condition['data'] ?>
            </span>
        </div>
        <h2>Forecast</h2>
        <? foreach ($forecast_list as $forecast) : ?>
        <div class="weather">
            <img src="<?= 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
            <div><?= $forecast->day_of_week['data']; ?></div>
            <span class="condition">
                <?= $forecast->low['data'] ?>&deg; F - <?= $forecast->high['data'] ?>&deg; F,
                <?= $forecast->condition['data'] ?>
            </span>
        </div>  
        <? endforeach ?>
    </body>
</html>

提前致谢。

【问题讨论】:

    标签: php web-services google-weather-api


    【解决方案1】:

    你可以创建一个表单

    <form method="post">
    City <input type="text" name="cityName">
    <br>
    <input type="submit">
    </form>
    

    并将$xml = simplexml_load_file('http://www.google.com/ig/api?weather=dhaka'); 更改为$xml = simplexml_load_file('http://www.google.com/ig/api?weather='.$_POST['cityName']);

    【讨论】:

      【解决方案2】:

      使用 html 表单,从 $_GET / $_POST 和 rawurlencode() 获取输入到 api url。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-06
        • 1970-01-01
        • 2015-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多