WeChat Public account Development mode PHP interface implementation

Source: Internet
Author: User
Tags cdata sha1 sprintf
<?phpdefine ("token", "Your Token"), class wechatcallbackapi{//public $signature;    Encrypted signature//public $timestamp;        Time Stamp//public $nonce;    Random number//public $echostr;    Random string, return flag public $fromUserName;        Sender ID public $toUserName;        The recipient ID (public number) is $keyWords;        Text information public $msgId; Message ID public $welcomeMsg;    Welcome message Public $msgType;        Message type public $event;    Event name Public $mediaId;        Voice message Media ID, you can call the multimedia file download interface to pull the media public $format;    Speech format: AMR public $recognition; Speech recognition Results Public function __construct () {$this->welcomemsg= "HELLO, Welcome to subscribe!            ";    $this->main (); Open when first verified, then close ***************}//First token verify public function main () {$echoStr =$_get[' E        Chostr '];        if ($this->checkres ()) {echo $echoStr;        }else{exit ();    }}//Verify Permissions Private Function Checkres () {$signature =$_get[' signature '); BeenFetch the service sent over the parameters $timestamp =$_get[' timestamp '];        $nonce =$_get[' nonce '];        $token = token;        $TMPARR = Array ($token, $timestamp, $nonce);    Sort ($TMPARR);    Sort the Dictionary $strWaitCheck = implode ($TMPARR);    String combination $strWaitCheck = SHA1 ($strWaitCheck);        SHA1 Encrypt the string if ($strWaitCheck = = $signature) {return true;        }else{return false;    }} Public Function Getwhatwechatserverpost () {$huntPostContent = $GLOBALS ["Http_raw_post_data"];    Get the service post to the content $newXmlObj =simplexml_load_string ($huntPostContent, ' simplexmlelement ', libxml_nocdata);    Load the post string into the object $this->fromusername = $NEWXMLOBJ->fromusername;    Username $this->tousername = $newXmlObj of the message to the public account->tousername;    Public account name $this->keywords = Trim ($newXmlObj->content);        Message content $this->msgid = Trim ($newXmlObj->msgid); Reads the current message msgid $this->msgtype = tRim ($NEWXMLOBJ->msgtype);        Reads the current message type $this->event = Trim ($newXmlObj->event);    Reads the current event name $this->format = Trim ($newXmlObj->format);    Speech format: Amr $this->mediaid = Trim ($newXmlObj->mediaid);    Voice message Media ID, you can call the multimedia file download interface to pull the media $this->recognition = Trim ($newXmlObj->recognition);    Speech recognition results, UTF8 encoding}//Message response Public Function responsemsg () {$this->getwhatwechatserverpost (); Executes the receiving server post over something//when the message type is text, handle if (!empty ($this->keywords) && Amp                            $this->msgtype = = "Text") {$para = $this->keywords; Switch ($para) {case "1": $tmp _item = $this->textp  Icmaxtmp ("This is a test!!", "Uncle Aunt don't too big, it's a test drip",                                  "Http://www.baidu.com/img/bdlogo.gif", "http://www.ba                                    idu.com/");                                    $re _global = $this->restextpicmax ($this->fromusername, $this->tousername,1, $tmp _item);                                    echo $re _global;                                Break                                    Case "2": $contentResText = "hello,buddy,you send me a \" 2\ "[Rose]";                                    $re _global = $this->restextmsg ($this->fromusername, $this->tousername, $contentResText);                                    echo $re _global;                                                                    Break                                    Case "": $contentResText = "The string you want to return";                                    $this->longstrhandleextendfunc ($contentResText);        Break                                          Default://Call function judgment function processing                                       $this->longstrhandle ($para);                                Break                                    }}else if ($this->msgtype = = "Event" &&!empty ($this->event)) {//Get event Type if ($this->event== ' subscribe ') {$contentResText    = $this->welcomemsg; Push the welcome message when you first focus $re _global = $this->restextmsg ($this->fromusername, $this-                                        >tousername, $contentResText);                                            }else if ($this->event = = "Unsubscribe") {///This place seems to have no effect!!!                                            $contentResText = "I rub, you run away";                       $re _global = $this->restextmsg ($this->fromusername, $this->tousername, $contentResText);                     }}else if ($this->msgtype = = "Voice") { if ($this->recognition = = "") {$contentResText = "Pro, you say the Martian text, do not understand, suggest you or the text query it. [Bared teeth];//Voice returns an error when no results are returned $re _global = $this->restextmsg ($this->fro                                            Musername, $this->tousername, $contentResText);                                                }else{$contentResText = $this->recognition;//Other cases return the Welcome language $re _global = $this->restextmsg ($this->fromusername, $this->tousername, $contentResT                                                                                                    EXT); }}else{//If you accidentally get an empty message in the text case                      The processing echo "";      Exit ();    } echo $re _global; Returns a value to the server}//A few words used several times defines a small method public function Longstrhandleextendfunc ($conText)                    {$re _global = $this->restextmsg ($this->fromusername, $this->tousername, $conText);                echo $re _global; }//receive the handling of characters other than a specific character, which is handled according to the specific logic you need to handle the public function Longstrhandle ($waitHandle STR) {/* Your code */}//Back Complex text information method public function restextmsg ($tu, $fu, $textContent) {//text message template $TEXTTPL = "&L T;xml> <tousername><! [cdata[%s]]></tousername> <fromusername><!                   [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime>         <msgtype><! [cdata[%s]]></msgtype> <content><! [cdata[%s]]></content> <FuncFlag>0</FuncFlag> &                    Lt;/xml> ";    $msgType = "text";        Message type $time = time ();    Timestamp $resServerStr = sprintf ($TEXTTPL, $tu, $fu, $time, $msgType, $textContent);            Replace the contents of the template return $resServerStr;                    }//Reply graphic public function Restextpicmax ($tu, $fu, $itemNum, $totalItemStr) {//Picture template $PICTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><!                                     [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [Cdata[%s]]></msgtype>                                    <ArticleCount>%s</ArticleCount> <Articles> "                                     . $totalItemStr.                    "</Articles> </xml>";    $msgType = "News";        Message type $time = time (); Timestamp $resServerStr = sprintf ($PICTPL, $this->fromusername, $this->tousername, $time, $msgType, $itemN    UM);            Replace the contents of the template return $resServerStr; }//graphic template handles public function textpicmaxtmp ($title, $des, $PICURL, $JUMPURL) {//text message entry modulo board $ITEMTPL = "<item> <title><! [cdata[%s]]></title> <description><! [cdata[%s]]></description> <picurl><! [CDATA[%s]] ></PicUrl> <url><!                [cdata[%s]]></url> </item> ";    $RESITEMSTR = sprintf ($ITEMTPL, $title, $des, $PICURL, $JUMPURL);                Replace the contents of the template return $resItemStr; }//The next step is your own definition of the various methods}
  • 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.