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"