WeChat public platform DEMO (PHP), Public demo

Source: Internet
Author: User

Public platform DEMO (PHP), Public demo

I set up the CI framework in the SAE environment (in fact, this little thing does not need to use the framework) and directly wrote the code in the controller.

<? Php if (! Defined ('basepath') exit ('no direct script access allowed'); // define your tokendefine ("TOKEN", "109 "); class Welcome extends CI_Controller {public function index () {/* $ this-> load-> helper ('url '); $ this-> load-> view ('Welcome _ message'); * // use chat response $ this-> responseMsg ();} // chat response public function responseMsg () {// get post data, May be due to the different environments $ postSt R = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post data if (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ MsgType = $ postObj-> MsgType; $ time = time (); switch ($ MsgType) {case ('text '): $ keyword = trim ($ postObj-> Content); $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> </xml> "; switch ($ keyword) {case (1 ): // Need to optimize // random read data from jokes $ SQL = 'select * FROM jok'; $ query = $ this-> db-> query ($ SQL ); $ res = $ query-> result_array (); $ num_rows = $ query-> num_rows (); $ key = rand (0, $ num_rows-1); // Notice: the value of key is from 0. // $ contentStr = $ key. '#'. $ res [$ key] ['content']; // debug $ contentStr = $ res [$ key] ['content']; Break; case (2): $ contentStr = 'your fromUsername is: '. $ fromUsername; break; case (3): $ newsTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <ArticleCount> 2 </ArticleCount> <Articles> <item> <Title> <! [CDATA [% s]> </Title> <Description> <! [CDATA [% s]> </Description> <PicUrl> <! [CDATA [% s]> </PicUrl> <Url> <! [CDATA [% s]> </Url> </item> <Title> <! [CDATA [% s]> </Title> <Description> <! [CDATA [% s]> </Description> <PicUrl> <! [CDATA [% s]> </PicUrl> <Url> <! [CDATA [% s]> </Url> </item> </Articles> </xml> "; $ resultStr = sprintf ($ newsTpl, $ fromUsername, $ toUsername, $ time, 'News', 'Baidu ', '', 'HTTP: // www.baidu.com/img/bdlogo.gif', 'HTTP: // www.baidu.com', 'Google ','','', 'http: // www.google.com '); // Notice: Google's logo is not suitable. echo $ resultStr; exit; // Notice: It's exit, not break. case (4): $ contentStr = "this function is under development. Please wait... "; break;/* others */def Ault: $ contentStr = "Reply number selection service \ n"; $ contentStr. = "1 joke featured \ n"; $ contentStr. = "2 get your Username... \ n "; $ contentStr. = "3 graphic message example \ n"; $ contentStr. = "4 developing... \ n "; break;} $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, 'text', $ contentStr); break; case ('image '): $ media_id = $ postObj-> MediaId; $ imgTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Image> <MediaId> <! [CDATA [% s]> </MediaId> </Image> </xml> "; $ resultStr = sprintf ($ imgTpl, $ fromUsername, $ toUsername, $ time, 'image', $ media_id); break; // try get the id of the receive image and analyze/* $ media_id = $ postObj-> MediaId; $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> </xml> "; $ length = strlen ($ media_id); $ contentStr =" I have got ed the image message you sent, the id of this image is # $ media_id #, and the length of media_id is # $ length # "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, 'text', $ contentStr); break; */case ('voice '): $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> </xml> "; $ contentStr = 'What do you mean? I can't hear you... '; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, 'text', $ contentStr); break;/* others */default: $ resultStr = "Input something... "; break;} echo $ resultStr;} else {echo"; exit ;}}}

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.