PHP micro-Credit Development text automatic reply _php example

Source: Internet
Author: User
Tags cdata php script sprintf

First of all, to the micro-trust public platform to register an account (registration to fill out a lot of things), after registration, log in. You can see the "Developer Center" on the left, and it seems like you need to improve some information before you start the Developer center, and follow the steps. After you get into the developer Center, edit it first.

To modify the configuration and modify the configuration, note:


The URL is a PHP script under your own domain name (read down to the demo of the script), which is used for docking with the micro-letter interface. Like http://www.example.com/weixin.php .

Token is a constant of the definitions in the script above, as defined in your PHP script:

Define ("TOKEN", "my_weixin");

So, when you fill out 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 of random generation can be.

When you are finished, save (prompt token validation fails if you save it, make sure token is consistent and try saving several times).

After saving, click the Modify configuration next to: "Open."

Then, you can edit your PHP script. (If you do not have your own domain name, you can use the free SAE of the Sina Cloud, and the best to complete the certificate)
The demo script is as follows: Focus on the public platform (such as after 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 '); Define ("TOKEN", "my_weixin");

Define your token $wx = new wechatcallbackapitest ();
  echatcallbackapitest{Public Function Valid () {//valid signature, option $ECHOSTR = $_get["Echostr"];
   if ($this->checksignature ()) {//Call validation field echo $echoStr;
  Exit The Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GLOBALS [ "Http_raw_post_data"]; Receive the micro-mail from the XML data//extract post data if (!empty ($POSTSTR)) {//parse post XML as an object $postobj $POSTOBJ = Simplexml_lo
  
   Ad_string ($postStr, ' simplexmlelement ', libxml_nocdata); $fromUsername = $POSTOBJ->fromusername; The user who requested the message $toUsername = $POSTOBJ->tousername; "I" public id $keyword = Trim ($postObj->content); Message content $time = time (); Time Stamp $msgtype = ' text '; Message type: Text $TEXTTPL = "<Xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[%s]]></msgtype> <content><!

  [cdata[%s]]></content> </xml> ";
    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
  }///Verify 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 mention a common service, please try again later. Then most of the code syntax is problematic, check good grammar errors and try again.

Report:

Automatically return information when new users focus on 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 wonderful content: Http://blog.csdn.net/misakaqunianxiatian ";
     $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
     echo $resultStr;
     Exit ();
    }
   

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.