This is from the Baidu API of a weather query interface, support history 7 days and the next 4 days of weather situation query, interface good, share to everyone.
Interface Request Address: http://apistore.baidu.com/apiworks/servicedetail/112.html
First of all, get a apikey (seemingly need to verify the phone verification code)
I am calling the interface for history 7 days and the next 4 days, the interface address is:
http://apis.baidu.com/apistore/weatherservice/recentweathers
I have integrated this weather query into the public platform to support the next 4 days of weather queries, including: Current temperature, PM value, wind direction, wind, maximum temperature, minimum temperature, weather status of the query, the effect is as follows:
Gossip not much to say, I would like to point directly, the core code is as follows:
1//Receive text messages2Privatefunction Receivetext ($object){3//Multiple spaces are shrunk to one4$keyword =$objectContent;5$keyword =Preg_replace ("/\s (? =\s)/", "\\1",$keyword);6$funcFlag = 0;7IfStrpos ($keyword, "")){8//Echo ' has spaces ';9$arr =Explode ("",$keyword);10If$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(' apikey:6512ad3a465e3a9e3d0d1da56639626c ',17);curl_setopt ($ch, Curlopt_httpheader,$header);curl_setopt ($ch, Curlopt_returntransfer, 1);curl_setopt ($ch, Curlopt_url,$url);21st$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 '] [' 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 [' Retdata '] [' forecast '];3435$out = "Today's temperature:".$citylowtemp. " ~".$cityhightemp. " \n\r "." Current temperature: ".$citytemp. " \n\r "." Weather conditions: ".$citytype. " \n\r "." PM2.5 value: ".$CITYPM. " \n\r wind Winds: ".$cityfengxiang. " ".$cityfengli;36$dateArray [] =Array ("Title" = =$cityname. " \n\r ".$citydate. " ".$cityweek, "Description" and "", "Picurl" and "" "," Url "and" "");37$dateArray [] =Array ("Title" = =$out, "Description" and "", "Picurl" and "" "," Url "and" "");38For$i = 0;$i <Count$cityforecast);$i + +){39$outstr = "? ".$cityforecast [$i ["Date"]. " ".$cityforecast [$i ["Week"]. " \n\r temperature: ".$cityforecast [$i ["Lowtemp"]. " ~".$cityforecast [$i ["Hightemp"]. " \n\r Wind: ".$cityforecast [$i ["Fengxiang"]. " ". $cityforecast [ $i] ["Fengli"]. " \n\r weather: ". $cityforecast [ $i] ["type" ]; The $dateArray [] = Array ("Title" => $outstr, "Description" and "", "picurl" = "", "Url" = "" ); + } ->transmitnews $resultStr = $this ( $object, $dateArray, $ Funcflag ); } () else {/// echo ' no spaces '; if ($keyword = = "Weather") {$dateArray = array (); $dateArray [] = Array ("Title" = "Query format error", "Description" = "", "" Picurl "+", "Url" and "" "); $dateArray [] = Array (" Title "and" correct format example: Weather Beijing "," Description "and" "," picurl "and" = " "," Url "="); $resultStr = $this->transmitnews ($object, $dateArray, $funcFlag);}52}53 return $resultStr; The
Query format: "Weather city Name"
The above describes the public number of weather query interface examples, including the public number of content, I hope that the PHP tutorial interested in a friend to help.