WeChat public number weather query interface instance WeChat public ID WeChat public number template WeChat public number name

Source: Internet
Author: User
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.

  • 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.