Introduction to the method of calling the API interface to query the weather function in PHP

黄舟
Release: 2023-03-16 09:58:01
Original
2784 people have browsed it

The following editor will bring you an example of using PHP to call the API interface to implement the weather query function. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.

Weather forecast query interface API. Here I use the National Meteorological Administration weather forecast interface

The most commonly used ones are: Sina Weather Forecast interface, Baidu weather forecast interface, Google weather interface, Yahoo weather interface, etc.

1. Query method

Query the weather conditions of each city based on place names

2.Request URL address

http://route.showapi.com/9-2
Copy after login

3. Interface parameter description:

1. System-level parameters (parameters required by all access points):

2. Application-level parameters (each access point has its own parameters):

4. Return parameters

Return results in JSON format

1) System-level parameters (all access Parameters that will be returned when clicking)

2) Application-level parameters (json data structure in the system-level output parameter showapi_res_body field)

Specific calling operation:

PHP comes with built-in functions for processing json format strings. Let’s do an example below and give the complete code:


 $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; ?> //将所需要的数据添加到数据库 query($sql); ?>
Copy after login

Effect As shown in the picture:

The above is the detailed content of Introduction to the method of calling the API interface to query the weather function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!