php+simplexml 车联网API(2) 天气预报的使用

WBOY
Release: 2016-06-23 13:37:31
Original
742 people have browsed it

<span style="font-size:18px;">weather.html页面<form action="weather.php" method="post">
<input type="text" name="city" value="查询的城市"><input type="submit" name="submit" value="查看天气">
</form>weather.php  xml处理页面<?php #编码设置	header("content-type:text/html;charset=utf-8");	#使用百度的libs接口进行实现	#判断是否正确提交	if(isset($_POST['submit'])){		#接收城市名		$city = $_POST['city'];				#对中文才是进行url转码		$city = urlencode($city);				#获取百度天气的api应用  密匙  0H6m5R2mBkUipA1zwDg0zBnG		$str = "http://api.map.baidu.com/telematics/v3/weather?location={$city}&ak=<span style="color:#993399;">0H6m5R2mBkUipA1zwDg0zBnG</span>";				#加载 获得xml文本		$xmlStr = file_get_contents($str);				#加载xmlStr文本		$data = simplexml_load_string($xmlStr);				#对信息进行遍历输出		foreach($data->results as $val){				echo '<pre class="brush:php;toolbar:false">';		       #当前所在城市  对照xml信息表,进行			echo '所在城市:'.$val->currentCity."     <img  src="%7B%24val->weather_data->dayPictureUrl%7D" alt="php+simplexml 车联网API(2) 天气预报的使用" ><br>";			echo '时间:'.$data->date."<br>";			echo '气温:'.$val->weather_data->temperature ."<br>";			echo '风力:'.$val->weather_data->wind.'<br>';			echo '空气质量:'.$val->pm25."<br>";			echo '生活指数:'.$val->index->title.'   **   '.$val->index->des.'<br>';			echo '大况:'.$val->weather_data->weather."<br>";					}	}
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template