Public Account weather query interface instance, Public Weather query instance
This is an interface for weather query from Baidu API. It supports weather query over the past seven days and the next four days. This interface is good and is shared 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'] ['t Oday'] ['Week ']; 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 ['retd Ata '] ['forecast']; 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 <count ($ cityforecast); $ I ++) {39 $ outstr = "". $ cityforecas T [$ I] ["date"]. "". $ cityforecast [$ I] ["week"]. "\ n \ r temperature :". $ cityforecast [$ I] ["lowtemp"]. "~ ". $ Cityforecast [$ I] ["hightemp"]. "\ n \ r wind power :". $ cityforecast [$ I] ["fengxiang"]. "". $ cityforecast [$ I] ["fengli"]. "\ n \ r weather :". $ cityforecast [$ I] ["type"]; 40 $ dateArray [] = array ("Title" => $ 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"