在链接中获取输入
这是一个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'] ?>° 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>
提前致谢。
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=dhaka
Then,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个表单
并将
$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
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']);
使用 html 表单,从 $_GET / $_POST 获取输入并将 rawurlencode() 转换为 api url。
Use html forms, get input from $_GET / $_POST and rawurlencode() it to api url.