WeChat public platform weather forecast function development _ php instance

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

The code description is provided. Users can send "Beijing weather" and "Beijing weather", so they first perform string processing to get the correct city name. Use the URL provided by Baidu Map API to send a GET request (which is actually a direct access) to obtain a json data packet. Convert the data packet information into an array format and reply to the user. There is no specific description of the weather in foreign countries, so I made some minor changes.

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.