PHP Call API interface to implement weather query function

Source: Internet
Author: User

Weather forecast query Interface API, here I am using the National Weather forecast interface

Use more also: Sina weather interface, Baidu weather interface, Google weather interface, Yahoo weather interface and so on.

1. Inquiry method

Check the weather conditions of each city according to place names

2. Request URL Address
Http://route.showapi.com/9-2

3, interface parameter description:

One, system-level parameters (parameters required for all access points):

Second, the application-level parameters (each access point has its own parameters):

4. Return parameters

Return results in JSON format

1) system-level parameters (parameters returned by all access points)

2) application-level parameters (JSON data structures in the system-level output Parameters showapi_res_body field)

Specific invocation actions:

PHP has its own built-in function for working with JSON-formatted strings, and here's a case, with the full code:

<?PHP
Find Zibo weather conditions//an array of the interface's own writing$showapi _appid= ' 46435 ';//Replace this value and find the relevant value in my apps on the official website$showapi _secret= ' 7c55aef4ede442ffa49b24c2c808e523 ';//Replace this value and find the relevant value in my apps on the official website$PARAMARR=Array( ' Showapi_appid ' =$showapi _appid, ' Areaid ' and ', ' area ' and ' Zibo ', ' needmoreday ' and ' ', ' needindex ' and ', ' needhourdata ' = "", ' need3hourforcast ' and ' ", ' needalarm ' and '" "//Add additional parameters);//create an array of parameters (including signature processing) that the interface has writtenfunctionCreateparam ($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;//sequence parameters plus secret, MD5 $sign=Strtolower(MD5($signStr)); $paraStr. = ' showapi_sign= '.$sign;//use the value after MD5 as a parameter for easy server validation return $paraStr;}$param= Createparam ($PARAMARR,$showapi _secret);$url= ' http://route.showapi.com/9-2? '.$param;
//Get data in JSON format $result=file_get_contents($url);
//Encode a string in JSON format $arr= (Json_decode ($result));$v=$arr-Showapi_res_body;$attr = call $v->f1;//Required Data $arr1 = $attr->day_weather; $arr 2 = $attr->night_weather; $arr 3 = $attr night_air_temperature; $arr 4 = $attr->day_air_temperature; $arr 5 = $attr->day_wind_direction; $arr 6 = $attr Night_weather_pic;echo $arr 6;
? >
Add the required data to the database
<?php
Require_once "./dbda.class.php";
$db = new Dbda ();

$sql = "INSERT into weather values (', ' {$arr 1} ', ' {$arr 2} ')";
$arr = $db->query ($sql);
?>

Effect

PHP Call API interface to implement weather query function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.