Public platform intelligent robot PHP Development example. Now there are more and more merchants using the public platform. the public platform provides me with a large number of api interfaces that can be used to interconnect with our website data, then, the user's input content is automatically returned and now more and more merchants are using the public platform. the public platform provides me with a large number of api interfaces that can be connected to our website data, then the user enters the content to automatically reply to the relevant information. next I will introduce the public platform intelligent machine development tutorial.
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. take "http: // www. your domain name. com/weixin/index. php" as an example.
TOKEN: enter it as needed to generate a signature. take "your domain name" as an example.
After entering the information, save the following code as index. php and upload it to the http: // www. your domain name. com/weixin/Directory. then click "submit" to complete the verification.
The code is as follows: |
|
Define ("TOKEN", "your domain name"); // 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:
The code is as follows: |
|
toUser fromUser 12345678 content 1234567890123456
|
After processing the message, the developer must return the data to the server:
The code is as follows: |
|
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:
The code is as follows: |
|
$ WechatObj = new wechat (); $ WechatObj-> responseMsg (); Class wechat { Public function responseMsg (){ // ---------- Collect data ----------// $ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // Get POST data // Use SimpleXML to parse the XML data after POST $ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA ); $ FromUsername = $ postObj-> FromUserName; // Obtain the sender's account (OpenID) $ ToUsername = $ postObj-> ToUserName; // Get the recipient's account $ Keyword = trim ($ postObj-> Content); // get the message Content $ Time = time (); // Obtain the current timestamp // ---------- Return data ----------//
// Return message template $ TextTpl =" %s %s % S %s %s 0 "; $ MsgType = "text"; // message type $ ContentStr = 'http: // www. your domain name. com'; // 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. your domain name. com/weixin/Directory. if the file is not deleted, overwrite it directly.
Now, if you send any message through the public platform, the public account will return a message with the content "http: // www. your domain name. 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, just go to the code:
The code is as follows: |
|
$ WechatObj = new wechat (); $ WechatObj-> responseMsg (); Class wechat { Public function responseMsg (){ // ---------- Collect data ----------// $ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // Get POST data // Use SimpleXML to parse the XML data after POST $ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA ); $ FromUsername = $ postObj-> FromUserName; // Obtain the sender's account (OpenID) $ ToUsername = $ postObj-> ToUserName; // Get the recipient's account $ Keyword = trim ($ postObj-> Content); // get the message Content $ Time = time (); // Obtain the current timestamp // ---------- Return data ----------//
// Return message template $ TextTpl =" %s %s % S %s %s 0 "; $ MsgType = "text"; // message type $ ContentStr = 'http: // www. your domain name. com'; // return the message content // Format the message template $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ Time, $ msgType, $ contentStr ); Echo $ resultStr; // output result } } ?> |
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 ~
...