WeChat public platform Intelligent robot PHP Development Example _php Tutorial

Source: Internet
Author: User
Tags cdata openid php example
Now there are more and more businesses using the public platform, the public platform provides me with a lot of API interface he can achieve with our site data docking, and then user input content automatically reply related information, below I will introduce the public Platform Intelligent Machine Development tutorial.

Recently in the development of public platforms, a breath of writing more than 20 functions, very interesting ~



Today to share the development experience ~
The interface provided by the public platform is simple, first look at the message interaction process:



In a more popular way, the user sends the data to the developer----a developer who processes the message and returns data to the user using the Send message, which is as simple as when the data interaction is done through XML.

Let's write an example to develop an intelligent chat robot:

1. Register for Public platform account

Public platform:
https://mp.weixin.qq.com/


Note: At present, an ID card can only register two accounts, account name is related to the certification of V, please register carefully.

2. Request a server/virtual host
Children's shoes without a server/virtual host can be used by BAE and SAE, without much introduction.

3. Open Developer mode
The public platform has two modes, one is the edit mode (fool mode), simple but functional single. Another is the developer mode, which can be developed to implement complex functions. Two modes of mutual exclusion, it is obvious that login to the public platform and the "Advanced Features" menu to open developer mode.

4. Fill in the interface configuration information
Also configured in the Advanced Features menu, you need to configure two parameters:
URL: Developer app Access address, currently only supports 80 ports, "http://www. your domain name. com/weixin/index.php" as an example.
TOKEN: Fill it out, use it to generate a signature, take "your domain name" as an example.
When you are finished, save the following code as index.php and upload it to http://www. your domain name. com/weixin/directory, then click "Submit" to complete the verification.

code as follows copy code

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 public platform check URL is correct access, research code is not meaningful, the verification can delete files, it is not explained in detail, interested children shoes can view official documents.

Public Platform API Documentation:
http://mp.weixin.qq.com/wiki/index.php



5. Develop a public platform function
OK, as mentioned above, the public platform and developers of data interaction is done through XML, since the use of XML, of course, to follow the specification, so before embarking on the development of the official interface documentation provided by the XML specification, as an example of text messages:

When a user sends a message to a public account, the server posts some data to the developer:

The code is as follows Copy Code


toUser

fromUser

12345678



content

1234567890123456


Developers need to return data to the server after processing the message:

code as follows copy code

"!--receiver account (OpenID)-->
toUser
!--developer number-->

-!--Message creation time (integer)-->
12345678
!--Message category (text text message)-->
!--[cdata1]-->
!--reply message Content-->
content
!--star operation (the message the star has just received when the bit 0x0001 is flagged)-->
0

In addition to text messages, the public platform also enables users to send picture messages, geo-location messages, link messages, event pushes, and developers can reply to the public platform for music messages and text messages, and the various message XML specifications can also be found in the official documentation.

To take a look at the official PHP example, I did some streamlining:

The code is as follows Copy Code

$WECHATOBJ = new WeChat ();
$WECHATOBJ->responsemsg ();
Class WeChat {
Public Function responsemsg () {

----------Receive data----------//

$POSTSTR = $GLOBALS ["Http_raw_post_data"]; Get Post Data

Parse the post XML data with SimpleXML.
$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);

$fromUsername = $POSTOBJ->fromusername; Get Sender account (OpenID)
$toUsername = $POSTOBJ->tousername; Get the account of the receiving party
$keyword = Trim ($postObj->content); Get message Content
$time = time (); Gets the current time stamp


----------return Data----------//

Return message Template
$TEXTTPL = "
%s
%s
%s
%s
%s
0
";

$msgType = "text"; Message type
$CONTENTSTR = ' http://www. your domain name. com '; Return message Content

Formatting message templates
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername,
$time, $msgType, $CONTENTSTR);
Echo $resultStr; Output results
}
}
?>

Save the code as index.php and upload it to http://www. your domain name. com/weixin/directory, if you have not deleted the file, overwrite it directly.

Now the user sends any message via the public platform the public account will return a message that is "http://www. com" for your domain name.
The next thing to do is to dynamically return results based on user messages ~

Simsimi (small yellow chicken) is the current comparison of fire chat robot, I used Curl developed a free simsimi (small yellow chicken) interface, the incoming keyword will return text reply, this part is not the focus of this article, not much explanation, directly on the code:

The code is as follows Copy Code

$WECHATOBJ = new WeChat ();
$WECHATOBJ->responsemsg ();
Class WeChat {
Public Function responsemsg () {

----------Receive data----------//

$POSTSTR = $GLOBALS ["Http_raw_post_data"]; Get Post Data

Parse the post XML data with SimpleXML.
$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);

$fromUsername = $POSTOBJ->fromusername; Get Sender account (OpenID)
$toUsername = $POSTOBJ->tousername; Get the account of the receiving party
$keyword = Trim ($postObj->content); Get message Content
$time = time (); Gets the current time stamp


----------return Data----------//

Return message Template
$TEXTTPL = "
%s
%s
%s
%s
%s
0
";

$msgType = "text"; Message type
$CONTENTSTR = ' http://www. your domain name. com '; Return message Content

Formatting message templates
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername,
$time, $msgType, $CONTENTSTR);
Echo $resultStr; Output results
}
}
?>

The above two pieces of code together is done, it is necessary to note that the server in 5 seconds to receive the response will be disconnected, through this interface may time out, and Simsimi has blocked the BAE and SAE crawl requests, recommended the use of Simsimi official API, Faster ~

http://www.bkjia.com/PHPjc/629894.html www.bkjia.com true http://www.bkjia.com/PHPjc/629894.html techarticle now there are more and more businesses using the public platform, and the public platform provides me with a lot of API interface he can achieve with our site data docking, and then user input content automatically back ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.