PHP WeChat public Platform Development-simple reply function Development _php Tutorial

Source: Internet
Author: User

"PHP Public Platform Development Series"

01. Configure the interface
02. Public Platform Sample Code analysis
03. Subscription event (SUBSCRIBE) processing
04. Simple Reply Function development


First, Introduction

PHP public platform can be based on the information sent by users to judge, and then give the corresponding reply, with good interactivity. The following will simulate the simple reply function, according to this case, the developer can also basic understanding of the principle of interaction, for deeper development.

Second, the Thinking analysis

The user sends over the text information, we can extract the keyword, through the simple if...elseif...else ... Realize.

The key code is as follows:

if ($keyword = = "Hello") {$contentStr = "Hello";} ElseIf ($keyword = = "Suzhou") {$contentStr = "There is heaven, there is Suzhou and Hangzhou";} else{$CONTENTSTR = "Thank you for your attention" Zhuo Jin Suzhou "No.: Zhuojinsz";}

If the user sends "Hello", then reply "Hello", if the user sends "Suzhou", then reply "There is heaven, there is Suzhou and Hangzhou", other information, then reply to your welcome word.

Three, the 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 ($postS                TR, ' 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";            if ($keyword = = "Hello") {$contentStr = "Hello";            }elseif ($keyword = = "Suzhou") {$contentStr = "There is heaven, there is Suzhou and Hangzhou";            }else{$CONTENTSTR = "Thank you for your attention" Zhuo Jin Suzhou "No.: Zhuojinsz";            } $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 Eve"NT: ". $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; }}}?>

Iv. Testing



This address: http://www.phpchina.com/archives/view-43385-1.html
This series by Phpchina invited author @david_tang feeds, reprint please indicate the author information and this article address.

http://www.bkjia.com/PHPjc/739143.html www.bkjia.com true http://www.bkjia.com/PHPjc/739143.html techarticle "PHP Public Platform Development Series" 01. Configure interface 02. Public Platform Sample Code Analysis 03. Subscription event (subscribe) processing 04. Simple reply Function Development One, Introduction 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.