Development of weather forecast function on WeChat public platform

Source: Internet
Author: User
A school application was started in the previous section, which added various features. Weather forecasts are an essential part.

A school application was started in the previous section, which added various features. Weather forecasts are an essential part.

I originally wanted to obtain and process the information directly from the China weather network. Later I found it was too troublesome to handle the problem. I also wanted to obtain the city code of all the cities! Weather outside China is not supported !! (There are many graduates in our school going abroad, so I want to make a version that supports weather outside China)

Therefore, we considered directly calling others' APIs. At the beginning, we chose the interfaces that have been prepared by Fang Times studio (but there is no foreign interface ). It wasn't until one day when the weather in Beijing was zero (it was so hot that I could not go out )... Change! Change! Change!

Later, we finally found the most reliable interface-Baidu's weather API.

To put it bluntly, you need to apply for a dedicated key on the Baidu map API (it takes about one minute to complete, which is very convenient)
The Code is as follows:

// Graphic information 2 for weather [This is a graphic information template] $ tqTpl =" %s %s % S %s 5 <! [CDATA [% s]> %s %s %s <! [CDATA [% s]> %s %s %s <! [CDATA [% s]> %s %s %s <! [CDATA [% s]> %s %s %s <! [CDATA [% s]> %s %s %s <! [CDATA [% s]> %s %s %s "; // You can see up to five items = speechless at last...

PHP code

If (substr_count ($ keyword, 'Weather ')! = 0 & $ keyword! = 'Weather ') {$ geshu = substr_count ($ keyword, 'Weather'); $ t = explode ("Weather", $ keyword); for ($ I = 0; $ I <= $ geshu; $ I ++) {if ($ t [$ I]! = '') {$ City = $ t [$ I]; break ;}}$ mykey =" [change to your API key here] "; $ url =" http://api.map.baidu.com/telematics/v3/weather?location= ". $ City. "& output = json & ak = ". $ mykey; $ output = file_get_contents ($ url); $ contentStr = json_decode ($ output, true); if ($ contentStr ['status'] = 'success ') {$ T [0] ['title'] = $ contentStr ['date']. "". $ contentStr ['results'] [0] ['currentcity']. "Weather"; // $ T [0] ['picurl'] =" http://zhengwairen-try2.stor.sinaapp.com/xytq.jpg "; // $ T [0] ['url'] =" http://zhengwairen-try2.stor.sinaapp.com/xytq.jpg "; // The above two rows can be set to the top image you want if (is_array ($ contentStr ['result'] [0] ['index']) {$ T [2] ['title'] = "[pm2.5 ]". $ contentStr ['results'] [0] ['pm25']. "\ n ". "【". $ contentStr ['results'] [0] ['index'] [0] ['title']. "]". "(". $ contentStr ['results'] [0] ['index'] [0] ['zs']. ")". $ contentStr ['results'] [0] ['index'] [0] ['des ']; // The next row shows the Car Wash index, I don't think it's a good topic .. // $ T [2] ['title'] = $ T [2] ['title']. "\ n ". "【". $ contentStr ['results'] [0] ['index'] [1] ['title']. "](". $ contentStr ['results'] [0] ['index'] [1] ['zs']. ")". $ contentStr ['results'] [0] ['index'] [1] ['des ']; $ T [2] ['title'] = $ T [2] ['title']. "\ n ". "【". $ contentStr ['results'] [0] ['index'] [2] ['title']. "](". $ contentStr ['results'] [0] ['index'] [2] ['zs']. ")". $ contentStr ['results'] [0] ['index'] [2] ['des '];} else $ guowai = 1; for ($ I = 1, $ aaa = 0; $ I <= 5; $ I ++) {if ($ I = 2 & $ guowai! = 1) continue; if ($ guowai = 1 & $ I = 5) continue; $ T [$ I] ['title'] = $ contentStr ['results'] [0] ['Weather _ data'] [$ aaa] ['date']. "". $ contentStr ['results'] [0] ['Weather _ data'] [$ aaa] ['temperature ']. "". $ contentStr ['results'] [0] ['Weather _ data'] [$ aaa] ['Weather ']. "". $ contentStr ['results'] [0] ['Weather _ data'] [$ aaa] ['wind ']; $ T [$ I] ['picurl'] = $ contentStr ['results'] [0] ['Weather _ data'] [$ aaa] ['daypictureurl']; $ T [$ I] ['url'] = $ contentStr ['results'] [0] ['Weather _ data'] [$ aaa] ['daypictureurl']; $ aaa ++ ;}$ tianqi = sprintf ($ tqTpl, $ fromUsername, $ toUsername, time (), "news", $ T [0] ['title'], $ T [0] ['description'], $ T [0] ['picurl'], $ T [0] ['url'], $ T [1] ['title'], $ T [1] ['description'], $ T [1] ['picurl'], $ T [1] ['url'], $ T [2] ['title'], $ T [2] ['description'], $ T [2] ['picurl'], $ T [2] ['url'], $ T [3] ['title'], $ T [3] ['description'], $ T [3] ['picurl'], $ T [3] ['url'], $ T [4] ['title'], $ T [4] ['description'], $ T [4] ['picurl'], $ T [4] ['url'], $ T [5] ['title'], $ T [5] ['description'], $ T [5] ['picurl'], $ T [5] ['url']); echo $ tianqi ;}}

As follows:

Change the $ mykey variable to your own APIkey. What makes me puzzled is that the image and text templates I use are obviously 6 items. Why are there only 5 items actually replied?

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.