WeChat development helps you quickly query city weather

Source: Internet
Author: User
Tags sunrise time
To query city weather, you must first find an API for weather retrieval. here, Baidu's apistore is used, you can go to Baidu to apply for and use API http://www.php.cn/php/php-tp-demand.html "target =" _ blank "> query city weather, first of all, you need to find an API to get the weather, here, using Baidu's apistore, you can go to Baidu to apply for and use APIs.

Log on to the Baidu account and send a mobile phone request to obtain the apikey. With apikey, you can request city weather according to its example. (Query by Chinese city name, pinyin, and City number)

Now you can perform a local test. after the request is complete, put it in your domain name space script.

The test script is as follows: (note that you have entered your own application for apikey)

Header ('content-type: text/html; charset = UTF-8 '); $ ch = curl_init (); $ url = 'http: // apis.baidu.com/apistore/weatherservice/cityname? Cityname = Shanghai'; $ header = array ('apikey: ', // your apikey); // add the apikey to headercurl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // execute the HTTP request curl_setopt ($ ch, CURLOPT_URL, $ url); $ res = curl_exec ($ ch ); $ res = json_decode ($ res, true); echo"
";print_r($res);echo "
"; $ ContentStr =" "; foreach ($ res as $ k = >$ v) {if ($ k =" retData ") {$ contentStr =" city :". $ v ['city']. "\ n"; $ contentStr. = "date :". $ v ['Date']. "\ n"; $ contentStr. = "weather :". $ v ['weate']. "\ n"; $ contentStr. = "average temperature :". $ v ['temp ']. "℃ \ n"; $ contentStr. = "minimum temperature :". $ v ['l _ tmp ']. "℃ \ n"; $ contentStr. = "Maximum temperature :". $ v ['H _ tmp ']. "℃ \ n"; $ contentStr. = "Wind power :". $ v ['Ws ']. "\ n"; $ contentStr. = "wind direction :". $ v ['wd ']. "\ n"; $ contentStr. = "sunrise time :". $ v ['Sunrise ']. "\ n"; $ contentStr. = "sunset time :". $ v ['Sunset']. "\ n"; $ contentStr. = "Longitude :". $ v ['longyun']. "\ n"; $ contentStr. = "latitude :". $ v ['latitude '] ;}} echo $ contentStr;

If you enter your own apikey, you should be able to get the requested weather:

If you can return normal data, you can put it in your domain name space. (The url entered by the developer center on the public platform, which has functions such as connection interfaces)

Public function responseMsg () {// get post data, May be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // receive XML data // extract post data if (! Empty ($ postStr) {// Parse the XML from post as an object $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA ); $ fromUsername = $ postObj-> FromUserName; // The user requesting the message $ toUsername = $ postObj-> ToUserName; // "my" public id $ keyword = trim ($ postObj-> Content); // the message Content sent by the user $ time = time (); // timestamp $ msgtype = 'text'; // Message Type: text $ textTpl ="
  
  %s
   
  %s
   
  
   
% S
   
  %s
   
  %s
   
 "; If ($ postObj-> MsgType = 'event') {// if the message type in XML is event if ($ postObj-> event = 'subscribe ') {// if it is a subscription event $ contentStr = "Welcome to subscribe to misaka last summer! \ N more highlights: http://blog.csdn.net/misakaqunianxiatian "; $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();}} $ which = mb_substr ($ keyword, 0, 2, 'utf-8'); // obtain the information to be returned if ($ which = "translation ") {// If you want to translate, // call the Youdao translation API for translation} elseif ($ which = "weather") {$ wea = $ which; $ city = str_replace ($ wea, "", $ keyword); $ ch = curl_init (); $ url =' http://apis.baidu.com/apistore/weatherservice/cityname?cityname= '. $ City; $ header = array ('apikey: '); // The apikey here uses the applied apikey, and enter it after the colon. // add the apikey to the header curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // execute the HTTP request curl_setopt ($ ch, CURLOPT_URL, $ url ); $ res = curl_exec ($ ch); $ res = json_decode ($ res, true); $ contentStr = ""; foreach ($ res as $ k => $ v) {if ($ k = "retData") {$ contentStr = "city :". $ v ['city']. "\ n"; $ contentStr. = "date :". $ v ['Date']. "\ n"; $ contentStr. = "weather :". $ v ['weate']. "\ n"; $ contentStr. = "average temperature :". $ v ['temp ']. "℃ \ n"; $ contentStr. = "minimum temperature :". $ v ['l _ tmp ']. "℃ \ n"; $ contentStr. = "Maximum temperature :". $ v ['H _ tmp ']. "℃ \ n"; $ contentStr. = "Wind power :". $ v ['Ws ']. "\ n"; $ contentStr. = "wind direction :". $ v ['wd ']. "\ n"; $ contentStr. = "sunrise time :". $ v ['Sunrise ']. "\ n"; $ contentStr. = "sunset time :". $ v ['Sunset']. "\ n"; $ contentStr. = "Longitude :". $ v ['longyun']. "\ n"; $ contentStr. = "latitude :". $ v ['latitude '] ;}}$ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();} else {$ contentStr = "input translation XXX can be translated (= ~ω ← =) \ n input weather XX to query city weather "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgtype, $ contentStr); echo $ resultStr; exit ();}

After that (do not forget to fill in apikey), enter the weather Shanghai in your subscription number, so you should be able to check the weather in Shanghai on that day.

The above is the development guide that allows you to quickly query the details of city weather. For more information, see other related articles on php Chinese network!

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.