Home > Backend Development > PHP Tutorial > Introduction to the method of calling the API interface to query the weather function in PHP

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

黄舟
Release: 2023-03-16 09:58:01
Original
2903 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:


<?php
//查找淄博天气情况
//接口自带编写的数组
$showapi_appid = &#39;46435&#39;; //替换此值,在官网的"我的应用"中找到相关值
$showapi_secret = &#39;7c55aef4ede442ffa49b24c2c808e523&#39;; //替换此值,在官网的"我的应用"中找到相关值 
$paramArr = array(
   &#39;showapi_appid&#39;=> $showapi_appid,
   &#39;areaid&#39;=> "",
   &#39;area&#39;=> "淄博",
   &#39;needMoreDay&#39;=> "",
   &#39;needIndex&#39;=> "",
   &#39;needHourData&#39;=> "",
   &#39;need3HourForcast&#39;=> "",
   &#39;needAlarm&#39;=> ""
   //添加其他参数
);

//创建参数(包括签名的处理)接口自带编写的数组
function createParam ($paramArr,$showapi_secret) {
   $paraStr = "";
   $signStr = "";
   ksort($paramArr);
   foreach ($paramArr as $key => $val) {
     if ($key != &#39;&#39; && $val != &#39;&#39;) {
       $signStr .= $key.$val;
       $paraStr .= $key.&#39;=&#39;.urlencode($val).&#39;&&#39;;
     }
   }
   $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
   $sign = strtolower(md5($signStr));
   $paraStr .= &#39;showapi_sign=&#39;.$sign;//将md5后的值作为参数,便于服务器的效验
  
   return $paraStr;
}

$param = createParam($paramArr,$showapi_secret);
$url = &#39;http://route.showapi.com/9-2?&#39;.$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;
?>
//将所需要的数据添加到数据库
<?php
require_once "./DBDA.class.php";
$db = new DBDA();

$sql = "insert into weather values(&#39;&#39;,&#39;{$arr1}&#39;,&#39;{$arr2}&#39;)";
$arr = $db->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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template