More and more businesses are using the WeChat public platform now. The WeChat public platform provides me with a large number of API interfaces that can be connected with our website data, and then the user enters content and automatically replies with relevant information. Let me introduce WeChat public platform. Platform intelligent machine development tutorial.
I have been working on WeChat public platform development recently, and I wrote more than 20 functions in one breath, which is quite interesting~
Let me share my development experience today~
The interface provided by WeChat public platform is very simple. Let’s take a look at the message interaction process first:
To put it more simply, users use WeChat to send messages -> WeChat sends the data to the developer -> The developer processes the message and returns the data to WeChat -> WeChat sends the returned data to the user, during which data interaction is completed through XML. It's that simple.
Write an example below to develop a WeChat intelligent chatbot:
1. Register a WeChat public platform account
WeChat public platform:
https://mp.weixin.qq.com/
Note: Currently, only two accounts can be registered with one ID card. The account name is related to the V certification, so please register carefully.
2. Apply for server/virtual host
Children's shoes without a server/virtual host can use BAE and SAE, which I won't introduce in detail.
3. Enable developer mode
The WeChat public platform has two modes, one is the editing mode (fool 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 in to the WeChat public platform and turn on the developer mode through the "Advanced Functions" menu.
4. Fill in the interface configuration information
It is also configured in the "Advanced Function" menu, and two parameters need to be configured:
URL: Developer application access address, currently only supports port 80, take "http://www.yourdomain.com/weixin/index.php" as an example.
TOKEN: Fill in whatever you want to generate a signature, take "your domain name" as an example.
After filling in, save the following code as index.php and upload it to the directory http://www.yourdomain.com/weixin/, and finally click "Submit" to complete the verification.
The code is as follows |
Copy code |
代码如下 |
复制代码 |
define("TOKEN", "你的域名"); //TOKEN值
$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;
}
}
}
?>
|
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 thing is to verify whether the URL is correctly accessed by the WeChat public platform. There is no substantive meaning in studying the code. The file can be deleted after verification. I will not explain it in detail. Interested children can check the official documentation.
WeChat public platform API documentation:
http://mp.weixin.qq.com/wiki/index.php
5. Develop WeChat public platform functions
OK, as mentioned above, the data interaction between the WeChat public platform and developers is completed through XML. Since XML is used, of course, the specifications must be followed, so before starting development, take a look at the XML specifications provided by the official interface document. Text message as an example:
When a user sends a message to a WeChat public account, the WeChat server will POST some data to the developer:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
12345678
1234567890123456
|
12345678
1234567890123456
|
After processing the message, the developer needs to return the data to the WeChat server:
The code is as follows |
Copy code |
12345678
0
|
In addition to text messages, the WeChat public platform also supports users to send picture messages, location messages, link messages, and event push. Developers can also reply to music messages and graphic messages to the WeChat public platform. Various message XML specifications See also the official documentation.
Let’s take a look at an official PHP example. I’ve simplified it a bit:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$wechatObj = new wechat();
$wechatObj->responseMsg();
class wechat {
public function responseMsg() {
//---------- 接 收 数 据 ---------- //
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //获取POST数据
//用SimpleXML解析POST过来的XML数据
$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; //获取发送方帐号(OpenID)
$toUsername = $postObj->ToUserName; //获取接收方账号
$keyword = trim($postObj->Content); //获取消息内容
$time = time(); //获取当前时间戳
//---------- 返 回 数 据 ---------- //
//返回消息模板
$textTpl = "
%s
0
";
$msgType = "text"; //消息类型
$contentStr = 'http://www.你的域名.com'; //返回消息内容
//格式化消息模板
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,
$time,$msgType,$contentStr);
echo $resultStr; //输出结果
}
}
?>
|
$wechatObj = new wechat();
$wechatObj->responseMsg();
class wechat {
public function responseMsg() {
//---------- Receive data ---------- //
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //Get POST data
//Use SimpleXML to parse the XML data from POST
$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; //Get the sender account (OpenID)
$toUsername = $postObj->ToUserName; //Get the recipient account
$keyword = trim($postObj->Content); //Get message content
$time = time(); //Get the current timestamp
//---------- Return data ---------- //
//Return message template
$textTpl = "
%s
0
";
$msgType = "text"; //Message type
$contentStr = 'http://www.yourdomain.com'; //Return message content
//Format 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 directory http://www.yourdomain.com/weixin/. If the file was not deleted just now, overwrite it directly.
Now when users send any message through the WeChat public platform, the public account will return a message with the content "http://www.yourdomain.com".
The next thing you need to do is to dynamically return results based on user messages~
SimSimi (Little Yellow Chicken) is currently a popular chat robot. I developed a free SimSimi (Little Yellow Chicken) interface using CURL. Incoming keywords will return a text reply. This part is not the focus of this article, so I will not elaborate further. Directly upload the code:
The code is as follows
代码如下 |
复制代码 |
$wechatObj = new wechat();
$wechatObj->responseMsg();
class wechat {
public function responseMsg() {
//---------- 接 收 数 据 ---------- //
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //获取POST数据
//用SimpleXML解析POST过来的XML数据
$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; //获取发送方帐号(OpenID)
$toUsername = $postObj->ToUserName; //获取接收方账号
$keyword = trim($postObj->Content); //获取消息内容
$time = time(); //获取当前时间戳
//---------- 返 回 数 据 ---------- //
//返回消息模板
$textTpl = "
%s
0
";
$msgType = "text"; //消息类型
$contentStr = 'http://www.你的域名.com'; //返回消息内容
//格式化消息模板
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,
$time,$msgType,$contentStr);
echo $resultStr; //输出结果
}
}
?>
|
|
Copy code
|
$wechatObj = new wechat();
|
$wechatObj->responseMsg();
class wechat {
public function responseMsg() {
//---------- Receive data ---------- //
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //Get POST data
//Use SimpleXML to parse the XML data from POST
$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; //Get the sender account (OpenID)
$toUsername = $postObj->ToUserName; //Get the recipient account
$keyword = trim($postObj->Content); //Get message content
$time = time(); //Get the current timestamp
//---------- Return data ---------- //
//Return message template
$textTpl = "
%s
0
";
$msgType = "text"; //Message type
$contentStr = 'http://www.yourdomain.com'; //Return message content
//Format message template
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,
$time,$msgType,$contentStr);
echo $resultStr; //Output result
}
}
?>
Integrate the above two pieces of code and you are done. It needs to be explained that the WeChat server will disconnect if it does not receive a response within 5 seconds. There may be a timeout through this interface, and SimSimi has blocked BAE and SAE. For crawling requests, it is recommended to use SimSimi’s official paid API, which is faster~
http://www.bkjia.com/PHPjc/629894.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629894.htmlTechArticleMore and more businesses are using the WeChat public platform. The WeChat public platform provides me with a large number of API interfaces. It can be connected with our website data, and then the user input content will be automatically returned...