"PHP Public Platform Development Series"
01. Configure the interface
02. Public Platform Sample Code analysis
03. Subscription event (SUBSCRIBE) processing
This address: http://www.phpchina.com/archives/view-43367-1.html
This series by Phpchina invited author @david_tang feeds, reprint please indicate the author information and this article address.
First, Introduction
New users focus on the public platform and will generate a subscription event, the subscribe event , which does not respond appropriately to this event in the default code.
After the new user has focused on the public platform, it may be interesting to know what features the platform provides and how to use it, which is a "user's manual" for the platform.
This article will detail the processing of the Subscribe event, and respond to the corresponding information to promote interactivity.
Second, the Thinking analysis
Currently, there are five types of messages available, namely:
- Text message (textbox);
- Image message (image);
- Geolocation information (location);
- Linked message (link);
- Event push (events);
When you receive a message, you first need to make a judgment on the message type, and then make the processing for different types of messages. In the event push, the event type is divided into three kinds, subscribe (subscription), unsubscribe (unsubscribe), click (Custom menu click event), but also need to add another judgment, after judging as subscribe event, according to the set of welcome message, reply to the user.
Iii. Judging the type of message
$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata); $RX _type = Trim ($postObj->msgtype) Switch ($RX _type) {case "text": $resultStr = $this->handletext ($POSTOBJ); break; Case "Event": $resultStr = $this->handleevent ($POSTOBJ); break; Default: $resultStr = "Unknow msg type:". $RX _type; break;}
Description
$RX _type = Trim ($postObj->msgtype); Get the message type;
Case "Text":
$RESULTSTR = $this->handletext ($POSTOBJ); Use the Handletext () function to process text messages;
Case "Event":
$RESULTSTR = $this->handleevent ($POSTOBJ); Use the Handleevent () function to handle event pushes;
Iv. judging the type of event
Switch ($object->event) {case "subscribe": $contentStr = "Thank you for your interest in" Zhuo Jin Suzhou "." \ n "." Number: Zhuojinsz "." \ n "." Excellent splendid, famous city of Suzhou, we provide you with Suzhou Local life guide, Suzhou Related information query, do the best Suzhou platform. "." \ n "." The current platform features are as follows: "." \ n "." "1" Check the weather, such as enter: Suzhou Weather "." \ n "." "2" Check bus, such as input: Suzhou bus 178 "." \ n "." "3" translation, such as input: translation I love You "." \ n "." "4" Suzhou information query, such as input: Suzhou Guan Qian Street. \ n "." For more information, please look forward to ... "; break; Default: $contentStr = "Unknow Event:". $object->event; break;}
Description
If it is Subscribe event, set the reply content as "Thank you for your attention" Zhuo Jin Suzhou "...";
> Five, complete code
Responsemsg ();//$WECHATOBJ->valid (); class wechatcallbackapitest{/*public function valid () {$echoStr = $_ get["Echostr"]; Valid signature, option if ($this->checksignature ()) {echo $echoStr; Exit }}*/Public Function responsemsg () {//get-post data, May is due to the different environments $po STSTR = $GLOBALS ["Http_raw_post_data"]; Extract post Data if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr , ' SimpleXMLElement ', libxml_nocdata); $RX _type = Trim ($postObj->msgtype); Switch ($RX _type) {case "text": $resultStr = $this->handletext ($POSTOBJ); Break Case "Event": $resultStr = $this->handleevent ($POSTOBJ); Break Default $RESULTSTR = "Unknow msg type:" $RX _type; Break } Echo $resultStr; }else {echo ""; Exit }} Public Function Handletext ($POSTOBJ) {$fromUsername = $POSTOBJ->fromusername; $toUsername = $POSTOBJ->tousername; $keyword = Trim ($postObj->content); $time = time (); $TEXTTPL = "
%s
%s
%s
%s
%s
0
"; if (!empty ($keyword)) {$msgType = "text"; $CONTENTSTR = "Welcome to WeChat world!"; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR); Echo $resultStr; }else{echo "Input something ..."; }} Public Function Handleevent ($object) {$contentStr = ""; Switch ($object->event) {case "subscribe": $CONTENTSTR = "Thank you for your interest in" Zhuo Jin Suzhou "." \ n "." Number: Zhuojinsz "." \ n "." Excellent splendid, famous city of Suzhou, we provide you with Suzhou Local life guide, Suzhou Related information query, do the best Suzhou platform. "." \ n "." The current platform features are as follows: "." \ n "." "1" Check the weather, such as enter: Suzhou Weather "." \ n "." "2" Check bus, such as input: Suzhou bus 178 "." \ n "." "3" translation, such as input: translation I love You "." \ n "." "4" Suzhou information query, such as input: Suzhou Guan Qian Street. \ n "." For more information, please look forward to ... "; Break Default: $contentStr = "Unknow Event:". $object->event; Break } $resultStr = $this->responsetext ($object, $CONTENTSTR); return $resultStr; } Public Function RespoNsetext ($object, $content, $flag =0) {$TEXTTPL = "
%s
%s
%s
text
%s
%d
"; $RESULTSTR = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time (), $content, $flag); return $resultStr; } 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; }}}?>
http://www.bkjia.com/PHPjc/739147.html www.bkjia.com true http://www.bkjia.com/PHPjc/739147.html techarticle "PHP Public Platform Development Series" 01. Configure interface 02. Public Platform Sample Code Analysis 03. Subscribe to Events (subscribe) processing This article address: http://www.phpchina.com/archives .