Php-based WeChat development-Methods for receiving messages, automatically determining and replying to corresponding messages, and php messages

Source: Internet
Author: User

Php-based methods for receiving messages, automatically determining and replying to corresponding messages, and php messages

This article describes how to receive, automatically determine, and reply to, messages developed by php. We will share this with you for your reference. The details are as follows:

The api is now powerful enough to call the content of our website or specified database at will to automatically reply to the current push message. Here we will introduce you to a development note.

Send messages 500, to test the corresponding message. The menu mode must be upgraded to the service number, or the number of subscribers exceeds.

1. All messages sent by users are received through the developer URL you submit. Here, the subscription number can be used. All messages sent are in xml format and need to be parsed. The Code is as follows:

$ Data = $ GLOBALS ["HTTP_RAW_POST_DATA"]; if (! Emptyempty ($ data) {// receives and processes the message $ xml = (array) simplexml_load_string ($ data, 'simplexmlelement', LIBXML_NOCDATA); return $ xml ;}

The response also needs to return the XML data format.

2. Actively send a message to the user, query user information, create a custom menu (now the subscription number can also be used), and some advanced functions require obtaining appid and AppSecret to get access_token.

Access_token is used by each interface except for receiving information. The length of access_token is 7200 milliseconds and needs to be retrieved again.

For more information, see: http://mp.weixin.qq.com/wiki/index.php? Title = % E9 % A6 % 96% E9 % A1 % B5.

Currently, I have implemented the following functions: receiving messages, automatically determining and replying to relevant news, creating custom menus, clicking custom menus, and performing corresponding operations, user lists, and so on, I feel that the current development mode is no different from the subscription mode.

Here is a test example of the test response. The Code is as follows:

<? Php/*** wechat php test * // define your tokendefine ("TOKEN", "token on your application"); $ wechatObj = new wechatCallbackapiTest (); $ wechatObj-> valid (); class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; // valid signature, option if ($ this-> checkSignature () {$ this-> responseMsg (); exit;} public function responseMsg () {// get post data, may be due to the different environments $ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post data if (! Response ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ time = time (); $ msgType = "text"; $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [% s]> </MsgType> <Content> <! [CDATA [% s]> </Content> <FuncFlag> 0 </FuncFlag> </xml> "; if (! Emptyempty ($ keyword) {$ contentStr = $ this-> keyrep ($ keyword); if (emptyempty ($ contentStr) {$ contentStr = "You did it on purpose, no culture is really terrible "; // you mean it, no culture is really terrible;} // $ contentStr = @ iconv ('utf-8', 'gb2312 ', $ keyword); $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr ;} else {$ contentStr = 'it's terrible to have no culture. You can't type it! '; // $ This-> keyrep ($ keyword); // $ contentStr = @ iconv ('utf-8', 'gb2312', $ keyword ); $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr ;}} else {echo ""; exit ;}} private function checkSignature () {$ 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 ($ tmpStr = $ signature) {return true ;} else {return false ;}} function keyrep ($ key) {// return $ key; if ($ key = 'Hi' | $ key = '') {$ mt = mt_rand ); $ array = array (1 => 'suicide. Let's talk about it later... ', 2 =>' Please yell for me! ', 3 =>' I am streaking and have been running out of the service area ', 4 =>' my current position: WC; posture: squat; face: twitching; Status: using force .... ', 5 =>' Have dinner. If you are a handsome guy, please contact me later. If you are a beautiful girl ............... even if you are a pretty girl, I have to eat up first. ', 6 =>' in the bath ~ Decline watching !! Performance_^ 0 ', 7 =>' have bears?], I am going to trap and come back as soon as possible. ', 8 =>' Hello, I'm 500. Are you 250? ', 9 =>' Hello! Garbled, and then ', 10 =>' It's not that I ignore you, it's just that time is hard to resist! ', 11 =>' I didn't know what you just said. Please say it again! ', 12 =>' How Many Times ~~~ I will send it back to you several times. ', 13 =>' this person is dead, something is burning paper! ', 14 =>' Hey, don't worry... ', 15 =>' Hello. I am going to kill a few people and will be back soon. ', 16 =>' have I become a fairy? Please smoke? Buddha said that smoke and fire could not make a success? Fire and smoke cannot become fairy. ', 17 =>' do you want to talk to me? Do you really want to talk to me? Are you sure you want to talk about it? Do you have to say no? Let's talk about it. This is an automatic response. I can't see it ~ What if you don't get me back? '); Return $ array [$ mt];} if ($ key =' '| $ key =' Ah' | $ key = 'A ') {$ mt = mt_rand (); $ array = array (1 => 'start person? Is Genius good? Playful? Get all done. ', 2 =>' future path? I hope you can continue on your own? And I'm on the bus ', 3 =>' What is a joke? That's what I said to you now. ', 4 =>' do everyone say I'm ugly? In fact, I am not very good. ', 5 => 'a; how did pigs die? B; how do you know that you are not dead? ', 6 =>' Obama has killed two people with the same name as him? You need to be careful. ', 7 =>' are some people alive? Is he dead? Some people live? He's dead. ', 8 =>' "Sister, are you sitting on the bow? My brother and I walked on the shore. "It is said that it was silly people to sing songs. ', 9 =>' is there only two things in my life? Neither will this happen? That's not true. ', 10 =>' has passed this village? No such store? That's because there are branches. ', 11 =>' I thought you were just a ball? Unexpectedly? You are really a ball. ', 12 =>' You have finally arrived. I have been looking for you for N years. What have you done on Mars? Now I am going to Pluto and tell you something. Don't go away. ', 13 =>' You have the right to keep silence. Everything you say will be recorded as a storage disk. You can ask the proxy server. If you cannot afford the network, you will be allocated one. ', 14 =>' I am wanted by Interpol worldwide. If you have a message about this person, please call the local police number ', 15 =>' in the bath ~ Decline watching !! Listen _^ 0 ', 16 => 'Sorry, here is the mobile secretary. Please send another one, and I will contact you. Please send it twice, and I will contact you; don't try again, brother or younger brother, because I don't contact you either! ', 17 =>' actually I am in ~ What if you don't get me back? ', 18 =>' I didn't know what you just said. Please say it again! ', 19 =>' Hey, don't worry... '); Return $ array [$ mt];} if ($ key = 'Excuse me') {$ mt = mt_rand (1, 5 ); $ array = array (1 => '"My face oil" reflection ?? Anyway, I don't know ', 2 =>' Go, I invite you to dinner ', 3 =>' this person is dead, something to burn paper! ', 4 =>' Hello! What! Garbled, and then ', 5 =>' What is a joke ?? That's what I said to you now. '); Return $ array [$ mt];} return "";} function keylist () {$ array = array (1 => 'hi ', 2 => 'hello', 3 => 'depend on ', 4 => 'Are you?', 5 => 'Excuse me ') ;}?>

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.