天气预报查询接口API,在这里我使用的是国家气象局天气预报接口

使用较多的还有:新浪天气预报接口、百度天气预报接口、google天气接口、Yahoo天气接口等等。

1、查询方式

根据地名查询各城市天气情况

2.请求URL地址

http://route.showapi.com/9-2

3、接口参数说明:

一、系统级参数(所有接入点都需要的参数):

二、应用级参数(每个接入点有自己的参数):

4.返回参数

以JSON格式返回结果

1)系统级参数(所有接入点都会返回的参数)

2)应用级参数(系统级输出参数showapi_res_body字段中的json数据结构)

具体调用操作:

PHP中自带了处理json格式字符串的内置函数,下面做一个事例,并给出完整代码:

<?php//查找淄博天气情况//接口自带编写的数组$showapi_appid = "46435"; //替换此值,在官网的"我的应用"中找到相关值$showapi_secret = "7c55aef4ede442ffa49b24c2c808e523"; //替换此值,在官网的"我的应用"中找到相关值 $paramArr = array(   "showapi_appid"=> $showapi_appid,   "areaid"=> "",   "area"=> "淄博",   "needMoreDay"=> "",   "needIndex"=> "",   "needHourData"=> "",   "need3HourForcast"=> "",   "needAlarm"=> ""   //添加其他参数);//创建参数(包括签名的处理)接口自带编写的数组function createParam ($paramArr,$showapi_secret) {   $paraStr = "";   $signStr = "";   ksort($paramArr);   foreach ($paramArr as $key => $val) {     if ($key != "" && $val != "") {       $signStr .= $key.$val;       $paraStr .= $key."=".urlencode($val)."&";     }   }   $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5   $sign = strtolower(md5($signStr));   $paraStr .= "showapi_sign=".$sign;//将md5后的值作为参数,便于服务器的效验     return $paraStr;}$param = createParam($paramArr,$showapi_secret);$url = "http://route.showapi.com/9-2?".$param; //获取json格式的数据 $result = file_get_contents($url); //对json格式的字符串进行编码$arr = (json_decode($result));$v = $arr->showapi_res_body;$attr = $v->f1;//所需要的数据进行调用$arr1 = $attr->day_weather;$arr2 = $attr->night_weather;$arr3 = $attr->night_air_temperature;$arr4 = $attr->day_air_temperature;$arr5 = $attr->day_wind_direction;$arr6 = $attr->night_weather_pic;echo $arr6;?>//将所需要的数据添加到数据库<?phprequire_once "./DBDA.class.php";$db = new DBDA();$sql = "insert into weather values("","{$arr1}","{$arr2}")";$arr = $db->query($sql);  ?>

效果如图:

以上这篇PHP调用API接口实现天气查询功能的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持网页设计。