Query city weather in PHP WeChat development, and query city weather in php

Source: Internet
Author: User
Tags sunrise time

Query city weather in PHP development, and query city weather in php

PHP query city weather, first of all, you need to find an API to get the weather, here, I am using Baidu's apistore, apply for and use the API URL: http://apistore.baidu.com/apiworks/servicedetail/112.html

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 "<pre>"; print_r ($ res); echo "</pre>"; $ 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)
If you do not understand the following code or the first contact with development, you can refer to my previous article: http://www.bkjia.com/article/87252.htm

The following code is part of responseMsg:

Public function responseMsg () {<span style = "white-space: pre"> </span> // get post data, may be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // receives XML data from the queue // 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 = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> </xml> "; 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 = "") {// 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. c Om/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 all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.