This article mainly introduces the two modes (editing mode and development mode) of the public platform. As the name suggests, the editing mode is to write common functions, and the Development Mode has more functions, the following describes the development mode. For more information about the public platform development process, see the following illustration:
The public platform has two modes: editing mode and development mode.
Common functions can be done through the editing mode. The development mode has more functions. Let's use the development mode to develop helloword.
The procedure is as follows:
Step 1:Register a public account first (https://mp.weixin.qq.com)
Step 2:Register sae (http://sae.sina.com.cn/) as your server.
Step 3:Log on to the public platform (https://mp.weixin.qq.com) to view development documentation and download the official demo. Make appropriate changes.
Step 4:Compress the code into a zip file and upload it to the sae platform.
Step 5:Log on to the public platform and go to the Developer Center. Enable "service provider configuration ".
Step 6:Succeeded.
Start:
1. Register a Public Account (https://mp.weixin.qq.com) first)
2. Register sae (http://sae.sina.com.cn /)
After registration, remember to perform real-name authentication. Otherwise, when you bind your account to a public platform, you will always be prompted to "fail to get token. (Real-name authentication takes 3 working days to succeed)
Click Create application. After creation, you can see it below.
Go to the application you created. Click "code management.
3. login public platform (https://mp.weixin.qq.com)
View the development documentation and download the official demo.
The following code is displayed:
<?php/** * wechat php test *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$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 be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $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..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>
I have tried. The above Code does not seem to be able to execute the response part. So I made a change.
<? Php/*** wechat php test * // define your tokendefine ("TOKEN", "weixin"); $ wechatObj = new wechatCallbackapiTest (); // here the change if ($ _ GET ["echostr"]) {$ wechatObj-> valid ();} else {$ wechatObj-> responseMsg ();} class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; // valid signature, option if ($ this-> checkSignature ()) {echo $ echoStr; exit;} public function responseMsg () {// get p Ost data, May be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post data if (! Empty ($ postStr) {/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */libxml_disable_entity_loader (true ); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ 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... ";}} else {echo" "; exit ;}} private function checkSignature () {// you must define TOKEN by yourself if (! Defined ("TOKEN") {throw new Exception ('token is not defined! ');} $ Signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); // use SORT_STRING rule sort ($ tmpArr, SORT_STRING ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true ;} else {return false ;}}}?>
You can set Welcome to wechat world! Change to Hello Word!
4. compress the code into a zip file and upload it to the sae platform.
Click "edit code" to view the PHP file you uploaded. Right-click and view the url. Copy url (http://1.carlzhang.sinaapp.com/wx_carlzhang819.php ). Remember the tokens defined in this PHP file. Here is "weixin", which can be seen in the example.
5. log on to the public platform and go to the Developer Center. Enable "service provider configuration ". Enter the url copied in the previous step (here I deleted the previous 1 because my sae is the first version by default. You can try to delete 1. If url access is not reported as 404, it is OK ). The token is the token in the Code ("weixin" above ").
If it is enabled successfully, you can follow your platform and enter the content to see if the prompt is "Welcome to wechat world! Or Hello Word!
All of the above is an explanation of the public platform, hoping to help you.