First of all, first go to the public platform to register an account (registration to fill a lot of things), after registration, log in. You can see the "Developer Center" on the left, and it seems that you need to refine some information before opening the developer Center, and follow the steps. After entering the developer center, go to edit first
To modify the configuration, when modifying the configuration, note:
The URL is a PHP script under your own domain name (read the demo of the script below), which is used for interfacing with the interface. Like http://www.example.com/weixin.php .
Token is a constant of the definition in the above script, as defined in your PHP script:
Define ("TOKEN", "my_weixin");
So, when you fill in the token, you fill in the ABCDEFGH
Encodingaeskey is used for message encryption. You can write a 43 for the combination of numbers and letters, you can also choose "Random generation", the general choice randomly generated.
After filling, save (if the token verification fails when you save it, make sure the token is consistent and try to save it a few more times).
Once saved, click on "Open" next to modify configuration.
Then, you can edit your PHP script. (If you don't have your own domain name, you can use Sina Cloud's free SAE, and it's best to complete the real names certification)
The demo script is as follows: follow the public platform (such as the subscription number), the function is: input hehe return to Hello World!!! If you enter a different character, return the input heeh try.
Header (' Content-type:text/html;charset=utf-8 ');d efine ("TOKEN", "my_weixin"); Define your TOKEN$WX = new Wechatcallbackapitest (), if ($_get[' echostr ']) {$wx->valid ();//If a echostr is sent, verify}else{ $wx->responsemsg (); If there is no echostr, the message is returned}class wechatcallbackapitest{public Function valid () {//valid signature, option $ECHOSTR = $_get["EC Hostr "]; if ($this->checksignature ()) {//Call validation field echo $echoStr; Exit }} Public Function responsemsg () {//get-post data, May is due to the different environments $postStr = $GLOBALS ["Http_ Raw_post_data "]; Receives the outgoing XML data//extract the post data if (!empty ($POSTSTR)) {//parses the post to XML as an object $postobj $POSTOBJ = Simplexml_load_strin G ($postStr, ' simplexmlelement ', libxml_nocdata); $fromUsername = $POSTOBJ->fromusername; User $toUsername for Request message = $POSTOBJ->tousername; The "I" public number id $keyword = Trim ($postObj->content); Message content $time = time (); Timestamp $msgtype = ' text '; Message type: Text $TEXTTPL = "
%s
%s
%s
%s
%s
"; if ($keyword = = ' hehe ') {$contentStr = ' hello world!!! '; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $CONTENTSTR); Echo $resultStr; Exit (); }else{$contentStr = ' input hehe try '; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $CONTENTSTR); Echo $resultStr; Exit (); }}else {echo ""; Exit }}//Validation field 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;}}}
If you send a message, the system prompts: the public platform is temporarily unable to share services, please try again later. Then most of the code syntax is problematic, check the syntax errors and try again.
Report:
Automatically returns information when new users follow your public number: (Add this code before judging $keyword).
if ($postObj->msgtype = = ' event ') {//If the message type in the XML message is event if ($postObj->event = = ' Subscribe ') {//If it is a subscription event $ Contentstr = "Welcome to subscribe to Misaka last summer!" \ n More exciting content: Http://blog.csdn.net/misakaqunianxiatian "; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr; Exit (); } }
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the script home.