WeChat public Platform developer mode is enabled and automatically replied

Source: Internet
Author: User
Tags cdata
This article is about the public platform developer mode of activation and automatic response, has a certain reference value, now share to everyone, the need for friends can refer to

First, what is developer mode?

The developer mode is to verify your server address first, and once the user sends a message to the public, the user's message will be forwarded to the address. after your server has received the data, then you design a set of programs yourself , a result is output and returned to the user by the server.

Personal learning Development Recommended use test number

Test number address: Http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

After logging in, fill in the interface configuration information with the URL address and token that you just generated.

The URL address is the two-level domain name address.

Token is fixed to Weixin in the program

Fill in the submission, prompt configuration success!

if prompted " Token validation failed ", multiple times a few times.

<?php//define your Tokendefine ("TOKEN", "Weixin"); $wechatObj = new Wechatcallbackapitest (); $wechatObj->run ();         Class Wechatcallbackapitest{public function Run () {if ($this->checksignature () = = False) {die ("illegal request");            } if (Isset ($_get["Echostr")) {$echoStr = $_get["Echostr"];            Echo $echoStr;        Exit        }else{$this->responsemsg (); }} Public Function responsemsg () {//get post data, could be due to the different environments$poststr = $GLOBALS ["HTTP _raw_post_data "];//$postStr = file_get_contents (" Php://input "); File_put_contents (' Msg.txt ', $POSTSTR, File_append)      ; Extract Post Dataif (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', LIBX                Ml_nocdata);                $fromUsername = $POSTOBJ->fromusername;                $toUsername = $POSTOBJ->tousername;                $keyword = Trim ($postObj->content);   $time = time ();             $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 (!empty ($keyword)) {$msgType = "text"; $CONTENTSTR = "Hello!"                ";                $CONTENTSTR = "hi!";                $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);                Echo $resultStr;                }else{echo "Input something ...";        }}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;}}? >
Related Article

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.