This article mainly introduces the php-based public platform development portal. The example analyzes the registration, configuration methods, and interface code implementation skills of the public platform, which is very practical, for more information about php-based public platform development, see the examples in this article. Share it with you for your reference. The details are as follows:
I recently developed a public platform and wrote more than 20 functions in one breath, which is quite interesting ~
Let's share our development experience today ~
The interfaces provided by the public platform are simple. Let's take a look at the message interaction process:
Generally speaking, you can send a message-> send data to a developer-> process a message by a developer and return data to-> send the returned data to the user, during this period, data interaction is completed through XML.
Below is an example to develop an intelligent CHATBOT:
1. Register a public platform account
Public platform:
Https://mp.weixin.qq.com/
Note: currently, only two accounts can be registered for one ID card. The account name is subject to V authentication. Please register with caution.
2. Apply for servers/virtual hosts
BAE and SAE can be used for children's shoes without servers or virtual hosts.
3. Enable the developer Mode
The public platform has two modes: The editing mode (the dummies mode), which is simple but has a single function. The other is the developer mode, which can implement complex functions through development. The two modes are mutually exclusive. Obviously, log on to the public platform and enable the developer mode through the "advanced functions" menu.
4. Fill in interface configuration information
You also need to configure two parameters in the "advanced functions" menu:
URL: the developer's application access address. Currently, only port 80 is supported. The example is "http://www.yoonper.com/weixin/index.php.
TOKEN: enter it as needed to generate a signature. Take "YoonPer" as an example.
After entering the information, save the following code as index. php and upload it to the http://www.yoonper.com/weixin/directory. Click Submit to complete verification.
<? Phpdefine ("TOKEN", "YoonPer"); // TOKEN value $ wechatObj = new wechat (); $ wechatObj-> valid (); class wechat {public function valid () {$ echoStr = $ _ GET ["echostr"]; if ($ this-> checkSignature () {echo $ echoStr; 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 ;}}}?>
This is what the public platform verifies whether the URL is correctly accessed. The research Code has no substantive significance. After the verification, you can delete the file. This is not detailed. If you are interested, you can view the official documentation.
Public platform API documentation:
Http://mp.weixin.qq.com/wiki/index.php
5. Develop public platform Functions
OK, as mentioned above, the data interaction between the public platform and developers is completed through XML. Since XML is used, the rules must be followed, before proceeding with the development, let's take a look at the XML specifications provided in the official interface documentation. Take text messages as an example:
When a user sends a message to a public account, the server will POST some data to the developer:
toUser
fromUser
12345678
content
1234567890123456
After processing the message, the developer must return the data to the server:
toUser
fromUser
12345678
content
0
In addition to text messages, the public platform also allows users to send image messages, geographic location messages, link messages, and event pushes. developers can also reply music messages and text messages to the public platform, for more information about Message XML specifications, see the official documentation.
Let's take a look at an official PHP example. I have made some streamlining:
<? Php/* ------------------------------------------------- | index. php [public platform interface] + response | Author: limyonper + response */$ wechatObj = new wechat (); $ wechatObj-> responseMsg (); class wechat {public function responseMsg () {// ---------- receive data ---------- // $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // get POST data // parse post xml data with SimpleXML $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; // obtain the sender's account (OpenID) $ toUsername = $ postObj-> ToUserName; // obtain the recipient's account $ keyword = trim ($ postObj-> Content ); // get the message content $ time = time (); // get the current timestamp // ---------- return data ---------- // return message template $ textTpl ="
%s
%s
% S
%s
%s
0
"; $ MsgType =" text "; // Message Type include ('simsimi. php '); $ contentStr = simsimi ($ keyword); // return the message content // format the message template $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr); echo $ resultStr; // output result }?>
Save the code as index. php and upload it to the http://www.yoonper.com/weixin/directory. If the file is not deleted, the file will be directly overwritten.
Now users send any message through the public platform the public account returns a message with the content of "http://www.YoonPer.com.
What needs to be done next is to dynamically return results based on user messages ~
SimSimi (xiaohuang chicken) is currently a popular chatbot. I used CURL to develop a free SimSimi (xiaohuang chicken) interface, and a text reply will be returned when a keyword is passed in, this part is not the focus of this article, so I will not describe it much. Go directly to the Code (updated on January 28 ):
<? Php/* ------------------------------------------------- | simsimi. php [intelligent chat (simsimi)] + response | Author: limyonper + ------------------------------------------------ */function simsimi ($ keyword) {$ keyword = urlencode ($ keyword ); // ----------- get COOKIE ---------- // $ url =" http://www.simsimi.com/ "; $ Ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_HEADER, 1); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); $ content = curl_exec ($ ch ); list ($ header, $ body) = explode ("\ r \ n", $ content ); preg_match_all ("/set \-cookie :( [^ \ r \ n] *);/iU", $ header, $ matches); $ cookie = implode (';', $ matches [1]). "; simsimi_uid = 1;"; curl_close ($ ch); // ----------- capture and restore ---------- // $ url =" http://www.simsimi.com/ Func/reqN? Response "; $ ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_COOKIE, $ cookie ); $ content = json_decode (curl_exec ($ ch), 1); curl_close ($ ch); if ($ content ['result'] = '20140901 ') {return $ content ['sentence _ resp '];} else {return' I will not answer this question yet... ';}}?>
The above two pieces of code are integrated, and the connection will be disconnected if the server fails to receive the response within five seconds. This interface may cause timeout, simSimi has blocked the crawling requests on the BAE and SAES. We recommend that you use the official billing API of SimSimi, which is faster ~
I hope this article will help you develop php-based public platforms.