WeChat public account weather query interface instance-Guosheng Studio

Source: Internet
Author: User
Public Account weather query interface instance-Guosheng studio this is a weather query interface from Baidu API. it supports weather query over the past seven days and the next four days. The interface is good and I will share it with you.

Interface Application address: http://apistore.baidu.com/apiworks/servicedetail/112.html

First, obtain the apikey (it seems that the mobile phone verification code is required)

I am calling interfaces for the past 7 days and the next 4 days. the interface address is:

http://apis.baidu.com/apistore/weatherservice/recentweathers

I integrated the weather query function on the public platform to support weather query in the next four days, including: query the current temperature, pm value, wind direction, wind power, maximum temperature, minimum temperature, and weather status. The results are as follows:

I like to talk about it directly. the core code is as follows:

1 // received text information 2 private function receiveText ($ object) {3 // multiple spaces are reduced to a 4 $ keyword = $ object-> Content; 5 $ keyword = preg_replace ("/\ s (? = \ S)/"," \ 1 ", $ keyword); 6 $ funcFlag = 0; 7 if (strpos ($ keyword ,"")) {8 // echo 'Space '; 9 $ arr = explode ("", $ keyword); 10 if ($ arr [0] = "weather ") {11 $ dateArray = array (); 12 $ ch = curl_init (); 13 $ city = urlencode ($ arr [1]); 14 $ url =' http://apis.baidu.com/apistore/weatherservice/recentweathers?cityname= '. $ City; 15 $ header = array (16 'apikey: bucket', 17); 18 curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); 19 curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); 20 curl_setopt ($ ch, CURLOPT_URL, $ url); 21 $ res = curl_exec ($ ch); 22 $ data = json_decode ($ res, true ); 23 $ cityname = $ data ['retdata'] ['city']; 24 $ citydate = $ data ['retdata'] ['Today'] ['Date']; 25 $ cityweek = $ data ['retdata'] ['Today'] ['W Eek ']; 26 $ citytemp = $ data ['retdata'] ['Today'] ['curtemp']; 27 $ citypm = $ data ['retdata'] ['Today'] ['aqi']; 28 $ cityhightemp = $ data ['retdata'] ['Today'] ['hightemp ']; 29 $ citylowtemp = $ data ['retdata'] ['Today'] ['lowtemp ']; 30 $ citytype = $ data ['retdata'] ['Today'] ['type']; 31 $ cityfengli = $ data ['retdata'] ['Today'] ['fengli']; 32 $ cityfengxiang = $ data ['retdata'] ['Today'] ['fengxiang ']; 33 $ cityforecast = $ data ['retdata'] ['Fo Recast ']; 34 35 $ out = "today's temperature:". $ citylowtemp ."~ ". $ Cityhightemp. "\ n \ r ". "Current temperature :". $ citytemp. "\ n \ r ". "Weather condition :". $ citytype. "\ n \ r ". "PM2.5 value :". $ citypm. "\ n \ r wind direction :". $ cityfengxiang. "". $ cityfengli; 36 $ dateArray [] = array ("Title" => $ cityname. "\ n \ r ". $ citydate. "". $ cityweek, "Description" => "", "Picurl" => "", "Url" => ""); 37 $ dateArray [] = array ("Title" => $ out, "Description" => "", "Picurl" => "", "Url" => ""); 38 for ($ I = 0; $ I
 
  
$ Outstr, "Description" => "", "Picurl" => "", "Url" => ""); 41} 42 $ resultStr = $ this-> transmitNews ($ object, $ dateArray, $ funcFlag); 43} 44} else {45 // echo 'no space '; 46 if ($ keyword = "weather") {47 $ dateArray = array (); 48 $ dateArray [] = array ("Title" => "Query Format Error ", "Description" => "", "Picurl" => "", "Url" => ""); 49 $ dateArray [] = array ("Title" => "correct format example: weather in Beijing", "Description" => "", "Picurl" => "", "Url" => ""); 50 $ resultStr = $ this-> transmitNews ($ object, $ dateArray, $ funcFlag); 51} 52} 53 return $ resultStr; 54}
 

Query format: "Weather City name"

Related Article

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.