php获取天气实例

php获取天气实例

首先贴代码:


<html>
<head>
	<title>天气title>

head>
<body>
	<form action="" method="post">
		
<input type="text" name="address">
<input type="submit" name="" value="查询天气">


	form>
		
		if(!empty($_POST["address"])){
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,"https://www.sojson.com/open/api/weather/json.shtml?city={$_POST['address']}");
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);            
		curl_setopt($ch,CURLOPT_HEADER,0);

		$output = curl_exec($ch);
		//echo $output;
		$arry_json=json_decode($output,true);
		//var_dump($arry_json);
			?>
		<p>城市: echo $arry_json['city']; ?>p>
		<p>天气状况: echo $arry_json['data']['forecast'][0]['type'];?>p>
		<p>湿度: echo $arry_json['data']['shidu']; ?>p>
		 
			if(!empty($arry_json["data"]['pm25'])){
		echo "

PM2.5:".$arry_json['data']['pm25']."

"
;} ?>
if(!empty($arry_json["data"]['quality'])){ echo "

空气质量:".$arry_json['data']['quality']."

"
;} ?>
<p>气温: echo $arry_json['data']['wendu']; ?>p> } ?> body> html>

接口感谢:技术博客 提供免费天气api

结果如图:

php获取天气实例_第1张图片

界面简陋,自己备份。

你可能感兴趣的:(php)