如:通过下面的界面获取天气信息 源代码为:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>获取天气信息</title>
<script src="js/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<center>
<input type="text" id="city" /><button>查询天气</button>
<hr />
<div id="info">
</div>
</center>
</body>
<script type="text/javascript">
$(document).ready(function(){
$(document).on('click','button',function(){
var city = $("#city").val();
$.ajax({
type:"get",
url:"interface.php",
data:{
city:city
},
async:true,
success:function(response,status,xhr){
$("#info").html(response);
}
});
});
});
</script>
</html>
后台的服务程序
<?php
header("content-type:text/html;charset=utf-8");
$city = isset($_GET['city'])?$_GET['city']:'北京';
$urlJson = "http://wthrcdn.etouch.cn/weather_mini?city=北京";
$urlXML = "http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100";
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $urlXML);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
// curl_setopt($ch, CURLOPT_ENCODING, "gzip");
// $output = curl_exec($ch);
//
//
// curl_close($ch);
//
// $xml = simplexml_load_string($output);
// var_dump($xml);
$url="http://wthrcdn.etouch.cn/WeatherApi?city=$city";
try {
$xml = simplexml_load_file("compress.zlib://".$url);
}
catch (Exception $e) {
echo "稍后刷新";
}
$json=json_encode($xml);
$arr=json_decode($json,true);
//print_r($arr);
?>
<table border="0" >
<tr>
<th>日期</th>
<th>高温/低温</th>
<th>白天</th>
<th>夜间</th>
</tr>
<tr>
<td><?php echo $arr['yesterday']['date_1']?></td>
<td><?php echo $arr['yesterday']['high_1'].'/'.$arr['yesterday']['low_1']?></td>
<td><?php echo $arr['yesterday']['day_1']['type_1'].'-'.$arr['yesterday']['day_1']['fx_1'].'-'.$arr['yesterday']['day_1']['fl_1']?></td>
<td><?php echo $arr['yesterday']['night_1']['type_1'].'-'.$arr['yesterday']['night_1']['fx_1'].'-'.$arr['yesterday']['night_1']['fl_1']?></td>
</tr>
<?php foreach($arr['forecast']['weather'] as $key=>$val){?>
<tr>
<td><?php echo $val['date']?></td>
<td><?php echo $val['high'].'/'.$val['low']?></td>
<td><?php echo $val['day']['type'].'-'.$val['day']['fengxiang'].'-'.$val['day']['fengli']?></td>
<td><?php echo $val['night']['type'].'-'.$val['night']['fengxiang'].'-'.$val['night']['fengli']?></td>
</tr>
<?php }?>
</table>