在链接中获取输入

发布于 2024-12-20 09:49:02 字数 1870 浏览 2 评论 0原文

这是一个api:=>

http://www.google.com/ig/api?weather=[城市名称]

当我手动输入 [城市名称] 时,例如 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>

提前致谢。

This is an api:=>

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

When i manually put [city name] like http://www.google.com/ig/api?weather=dhakaThen,It's works perfectly.Is there any way to take user input for city name.

<?
$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'] ?>° 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'] ?>° F - <?= $forecast->high['data'] ?>° F,
                <?= $forecast->condition['data'] ?>
            </span>
        </div>  
        <? endforeach ?>
    </body>
</html>

Thanks in advance.

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

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

发布评论

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

评论(2

最冷一天 2024-12-27 09:49:02

您可以创建一个表单

<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']);

You could create a form

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

and change $xml = simplexml_load_file('http://www.google.com/ig/api?weather=dhaka'); to $xml = simplexml_load_file('http://www.google.com/ig/api?weather='.$_POST['cityName']);

淡笑忘祈一世凡恋 2024-12-27 09:49:02

使用 html 表单,从 $_GET / $_POST 获取输入并将 rawurlencode() 转换为 api url。

Use html forms, get input from $_GET / $_POST and rawurlencode() it to api url.

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