Weather forecast function for WeChat public platform development

Source: Internet
Author: User
This article will develop weather queries that everyone cares about every day. For example, if a user sends a message "Huanggang weather", the user will return the real-time weather conditions of Huanggang, and the weather conditions in the next two or even five days.

This article will develop weather queries that everyone cares about every day. For example, if a user sends a message "Huanggang weather", the user will return the real-time weather conditions of Huanggang, and the weather conditions in the next two or even five days.

Recently, a project needs to add the weather forecast function to the public account, and use the weather query function in iov api V3.0 provided by Baidu to achieve the following:

Project requirements: There is a connected platform and a registered Baidu account. You need to open the cloud platform on Baidu LBS, add applications, get AK code, PHP code editor, such as EditPlus.

The following describes the development procedure in detail:

Step 1: Prepare

Log on to the public platform and check whether the server configuration is enabled and whether the Token (Token) has been configured for the URL (server address) is consistent with the Token (Token) in the Self-written entry file, for example: click "Submit". Only the successful submission of the green background is displayed on the webpage. This step is complete.


Step 2: Prepare the weather forecast data source

Use a registered Baidu account, log on to the Baidu LBS cloud platform, add an application, obtain the accesskey of the application, and learn about the API V3.0 of IOV cc. The interface description file corresponding to the weather query function is provided, call the required weather information as needed.

Step 3: Compile jiekou. php In the public platform interface file

<? Php/* CAREFREE computer skills network public code function source code CopyRight 2015 All Rights Reserved */define ("TOKEN", "weixin2015"); $ wechatObj = new wechatCallbackapiTest (); if (! Isset ($ _ GET ['echostr']) {$ wechatObj-> responseMsg ();} else {$ wechatObj-> valid ();} class wechatCallbackapiTest {// verify the public function valid () {$ echoStr =$ _ GET ["echostr"]; $ signature =$ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); sort ($ tmpArr); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpSt R ==$ signature) {echo $ echoStr; exit ;}} public function responseMsg () {// $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; $ postStr = file_get_contents ("php: // input"); if (! Empty ($ postStr) {$ this-> logger ("R ". $ postStr); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ RX_TYPE = trim ($ postObj-> MsgType); $ result = ""; switch ($ RX_TYPE) {case "event": $ result = $ this-> receiveEvent ($ postObj); break; case "text ": $ result = $ this-> deleetext ($ postObj); break;} $ this-> logger ("T ". $ result); echo $ result;} else {echo ""; exit ;}} private funct Ion receiveEvent ($ object) {switch ($ object-> Event) {case "subscribe": $ content = "Welcome to worry-free computer skills network"; break ;} $ result = $ this-> transmitText ($ object, $ content); return $ result;} private function deleetext ($ object) {$ keyword = trim ($ object-> Content); // obtain user input information // determine the weather if (! Empty ($ keyword )){//! Empty function, judge whether the value obtained by $ keyword is null $ city = mb_substr ($ keyword, 0, 2, 'utf-8 '); // take the first two characters entered by the user. For example, the final value of "Huanggang weather" is "Huanggang" include ("weather. php "); // call the weather interface file $ content = getWeatherInfo ($ city); // execute the getWeatherInfo method in the weather interface file. query Huanggang weather .} else {$ content = date ("Y-m-d H: I: s", time ()). "\ n technical support worry-free computer skills network \ n"; // send other content default reply content .} if (is_array ($ content) {if (isset ($ content [0] ['picurl']) {$ result = $ this-> transmitNews ($ object, $ cont Ent);} else if (isset ($ content ['musicurl']) {$ result = $ this-> transmitMusic ($ object, $ content );}} else {$ result = $ this-> transmitText ($ object, $ content);} return $ result;} private function transmitText ($ object, $ content) {if (! Isset ($ content) | empty ($ content) {return "" ;}$ textTpl =" %s %s % S text %s "; $ Result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ content); return $ result ;} private function transmitNews ($ object, $ newsArray) {if (! Is_array ($ newsArray) {return "" ;}$ itemTpl =" <! [CDATA [% s]> %s %s %s "; $ Item_str =" "; foreach ($ newsArray as $ item) {$ item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['url']);} $ newsTpl =" %s %s % S news % S$ Item_str "; $ Result = sprintf ($ newsTpl, $ object-> FromUserName, $ object-> ToUserName, time (), count ($ newsArray); return $ result ;} private function logger ($ log_content ){}}

Step 4: Use the V3.0 API of Baidu automobile network and access the AK code of the application. The source code of the weather interface is:

Weatger. php

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.