制作一个属于自己的查询天气的API接口

2019年6月6日17:20:54 评论 4,795

想要自己制作一个查询天气的API接口,首先要获取天气的信息,可是我们是不可能去天天监测天气的,那么这时候就要用到国家气象局提供的天气预报接口了!

经过百度查询,有三个接口:

天气实况:http://www.weather.com.cn/data/sk/101010100.html

整天天气:http://www.weather.com.cn/data/cityinfo/101010100.html

天气指数:http://www.weather.com.cn/data/zs/101010100.html

这里只用整天天气为例制作,其他的举一反三

其中的101010100是城市ID,每一个城市分别有一个对应的ID

这里只介绍简单“通过城市名字查询天气信息”、“通过IP查询天气信息”以及“通过查询IP所在位置自动输出天气信息”的接口制作

(必要!!)获取所有城市与ID

这里提供一个文件"city.ini",里面囊括了所有直辖市的城市名字以及对应的ID,后面的教程将用到

通过城市名字查询天气

新建一个php文件,我这里测试使用"city.php"

利用parse_ini_file函数读取"city.ini"文件,并且设置使用GET请求来达到用城市名字查询ID

<?php
$city=$_GET["city"];
$a=parse_ini_file("city.ini");
echo $a[$city];
?>

这样就可以通过"http://example.com/city.php?city=城市名字"来查询ID

通过整天天气的官方解析接口来看,对我们有用的信息包括城市名[city]、最低温度[temp1]、最高温度[temp2]和天气情况[weather]

 

使用Curl来解析出这些信息,并且实现json输出。

GET请求中,city为查询的城市名称

代码如下

<?php
header('Content-Type:application/json; charset=utf-8');
$city=$_GET["city"];
$a=parse_ini_file("city.ini");
$cityid=$a[$city];
$tqurl="http://www.weather.com.cn/data/cityinfo/".$cityid.".html";
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';  
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $tqurl); 
curl_setopt($curl, CURLOPT_HEADER, 0);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
curl_setopt($curl, CURLOPT_ENCODING, '');  
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);  
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
$weatherinfo = curl_exec($curl);
$weatherinfo = json_decode($weatherinfo, true);
$weather = $weatherinfo['weatherinfo']['weather']; 
$temp1 = $weatherinfo['weatherinfo']['temp1']; 
$temp2 = $weatherinfo['weatherinfo']['temp2'];
$data = [
  'city' => $city,
  'time' => date("Y-m-d"),
  'temp1' =>$temp1,
  'temp2'=>$temp2,
  'weather'=>$weather,
];
exit(json_encode($data));
?>

 

通过IP查询天气信息

通过IP查询天气信息,就是在通过城市名字查询天气信息的基础上增加了查询IP所在的城市,这里利用的IP地址库为淘宝的IP地址库(ip.taobao.com)提供的API。

GET请求中,ip为查询的IP地址字串

代码如下:

<?php
header('Content-Type:application/json; charset=utf-8');
$ip=$_GET["ip"];
$a=parse_ini_file("city.ini");
//查询IP所在地
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; 
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';  
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_HEADER, 0);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
curl_setopt($curl, CURLOPT_ENCODING, '');  
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);  
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
$data = curl_exec($curl);
$data = json_decode($data, true);
$city = $data['data']['city'];
$cityid=$a[$city];
//查询天气信息
$tqurl="http://www.weather.com.cn/data/cityinfo/".$cityid.".html";
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';  
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $tqurl); 
curl_setopt($curl, CURLOPT_HEADER, 0);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
curl_setopt($curl, CURLOPT_ENCODING, '');  
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);  
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
$weatherinfo = curl_exec($curl);
$weatherinfo = json_decode($weatherinfo, true);
$weather = $weatherinfo['weatherinfo']['weather']; 
$temp1 = $weatherinfo['weatherinfo']['temp1']; 
$temp2 = $weatherinfo['weatherinfo']['temp2'];
$data = [
  'ip' => $ip,
  'city' => $city,
  'time' => date("Y-m-d"),
  'temp1' =>$temp1,
  'temp2'=>$temp2,
  'weather'=>$weather,
];
//$arr=array('city'=>1,''=>2);
exit(json_encode($data));
?>

 

通过查询IP所在位置自动输出天气信息

通过查询IP所在位置自动输出天气信息,就是在通过IP查询天气信息的基础上增加了自动获取访问者IP

代码如下:

<?php
header('Content-Type:application/json; charset=utf-8');
$ip=$_SERVER["REMOTE_ADDR"];
$a=parse_ini_file("city.ini");
//查询IP所在地
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; 
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';  
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_HEADER, 0);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
curl_setopt($curl, CURLOPT_ENCODING, '');  
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);  
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
$data = curl_exec($curl);
$data = json_decode($data, true);
$city = $data['data']['city'];
$cityid=$a[$city];
//查询天气信息
$tqurl="http://www.weather.com.cn/data/cityinfo/".$cityid.".html";
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';  
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $tqurl); 
curl_setopt($curl, CURLOPT_HEADER, 0);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
curl_setopt($curl, CURLOPT_ENCODING, '');  
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);  
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
$weatherinfo = curl_exec($curl);
$weatherinfo = json_decode($weatherinfo, true);
$weather = $weatherinfo['weatherinfo']['weather']; 
$temp1 = $weatherinfo['weatherinfo']['temp1']; 
$temp2 = $weatherinfo['weatherinfo']['temp2'];
$data = [
  'ip' => $ip,
  'city' => $city,
  'time' => date("Y-m-d"),
  'temp1' =>$temp1,
  'temp2'=>$temp2,
  'weather'=>$weather,
];
//$arr=array('city'=>1,''=>2);
exit(json_encode($data));
?>

 

weinxin
滑稽社
流星博客建立的官方交流群,包含技术交流|随意闲聊|好友扩列|热心管理在一体的技术群

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: