Home > Article > WeChat Applet > WeChat development tutorial series (1)
First of all, we need to apply for a WeChat public account. Because this public account belongs to the company, it is not convenient to disclose it to everyone! I believe that simply applying for a job will be no problem! After the application is successful, "Advanced Functions" will appear in the menu bar, as shown below:
There are 2 modes to choose from, "Edit Mode" and "Development Mode", above Since they are all described in detail, I don’t want you to explain them anymore!
Here we are going to talk about the development mode. Okay, now you can enter the explanation of development mode!
Then we configure the URL and Token values, as shown below:
URL: Fill in the access URL where we put the demo, for example: http://www .123.com/wx_sample.php
Token: This value can be written casually.
Open the file wx_sample.php and modify the following content
define("TOKEN", "weixin"); //修改成自己填写的token
After filling it in, you can submit it!
After successful verification, we can write a small example to test it, the following code:
<?php /** * wechat php test */ // define // your // token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; // valid // signature // , // option if($this->checkSignature()) { echo $echoStr; exit(); } } public function responseMsg() { // get // post // data, // May // be // due // to // the // different // environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; // extract // post // data if(!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_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 = "Welcome to wechat world!"; $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; } } } ?>
The simplest reply message is completed!
The above is the detailed content of WeChat development tutorial series (1). For more information, please follow other related articles on the PHP Chinese website!