The intelligent Reply Development course of PHP Micro-trust public platform Interface Development _php instance

Source: Internet
Author: User
Tags cdata php define sha1 sprintf

This paper illustrates the realization method of Intelligent reply function for the development of PHP version of micro-communication platform interface. Share to everyone for your reference, specific as follows:

Intelligent response is based on user input conditions to feedback results with the user, this small series has done a letter to organize some examples for your reference, the more complete is mainly introduced in the development end.

Micro-letter since the launch of a really fire, and the introduction of the payment function, and the micro-letter pushed to an incomparable height, and then apply for micro-letter subscription number or service number of people also began to hordes. Below I will give you a brief explanation of the micro-trust public platform development Interface.

First go to the micro-trust public platform to apply for an account, and then follow the prompts step by step. In the choice of subscription number and service number, the individual can only apply for the subscription number, and is limited to the basic functions, and the enterprise can apply both. The difference between a subscription number and a service number is: The subscription number can be a daily mass of a message, and the service number of one months to mass one; The subscription number requires a micro-letter authentication to customize the menu (Enterprise can be certified, certified 300 yuan a time), and the service number is the beginning of a custom menu, but also can be certified, After authentication, the service number is upgraded to advanced function directly. More differences please Baidu ...

I'm applying for a subscription number, because it's a person. As long as a hand holding the ID card on the head can be, although a little silly. Then wait for the information to be registered for approval (a day or so). After passing directly into the micro-trust public platform, click the function into advanced functions, turn off editing mode, open the development mode, and then download the micro-letter provided by the demo, decompression, on a file: wx_sample.php, the code is as follows:

<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
$WECHATOBJ = new Wechatcallbackapitest ();
$WECHATOBJ->valid ();
    Class Wechatcallbackapitest {public function valid () {$echoStr = $_get["Echostr"];
     Valid signature, option if ($this->checksignature ()) {echo $echoStr;
    Exit The Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GLOBALS [
    "Http_raw_post_data"]; Extract post Data if (!emptyempty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', LI
        Bxml_nocdata);
        $fromUsername = $POSTOBJ->fromusername;
        $toUsername = $POSTOBJ->tousername;
        $keyword = Trim ($postObj->content);
        $time = time (); $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> &LT;CREATETIME&GT;%S&LT;/CREATETIME&GT <msgtype><! [cdata[%s]]></msgtype> <content><!
  [cdata[%s]]></content> <FuncFlag>0</FuncFlag> </xml> ";
         if (!emptyempty ($keyword)) {$msgType = "text";
         $CONTENTSTR = "Welcome to WeChat world!";
         $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
        Echo $resultStr;
        }else{echo "Input something ...";
     }}else {echo "";
    Exit
    The Private Function checksignature () {$signature = $_get["signature"];
    $timestamp = $_get["timestamp"];
 $nonce = $_get["nonce"];
 $token = token;
 $TMPARR = Array ($token, $timestamp, $nonce);
 Sort ($TMPARR, sort_string);
 $TMPSTR = implode ($TMPARR);
 $TMPSTR = SHA1 ($TMPSTR);
 if ($tmpStr = = $signature) {return true;
 }else{return false;

 }}}?>

is actually authentication, and then sends the message. Upload the file to your server, I put it in the root directory, and then modify the URL and token values in development mode. If the URL used here is Http://www.jb51.net/wx_sample.php,token is the above define token, this can be changed, as long as the two sides remain consistent, the default is Weixin. Then click Submit and you will be prompted to succeed. Then sweep down the number of your application, send a message, you will find no response, this time we need a small adjustment, close the interface document call authentication method, open Call processing reply information method:

$WECHATOBJ->valid ();
$WECHATOBJ->responsemsg ();

This time you send another message, you will receive: Welcome to WeChat world!

If you are looking at some subscription numbers or service numbers, you will receive a message immediately. What reply 1, how how, reply 2, what kind of.

Take my own blog for example, my focus is:

Thank you for your attention Andyyang personal blog micro-letter assistant.
Reply "1" returns two latest articles
Reply "2" return two articles of popularity
Reply "3" returns two articles on hot review
Reply "4" returns two articles of the latest technology
Reply "5" to return two articles of the latest writing
Reply to other two articles that return search keywords
More wonderful content, do in: Www.jb51.net. Dear friends, please support Oh, thank you ~

So how does this happen? Directly on the code:

<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
$WECHATOBJ = new Wechatcallbackapitest ();
$WECHATOBJ->valid ();
$WECHATOBJ->responsemsg ();
    Class Wechatcallbackapitest {public function valid () {$echoStr = $_get["Echostr"];
      Valid signature, option if ($this->checksignature ()) {echo $echoStr;
    Exit The Public Function responsemsg () {//get post data, May is due to the different environments $postStr = $GL
    obals["Http_raw_post_data"]; Extract post Data if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', LIBXML
      _nocdata);
      $fromUsername = $POSTOBJ->fromusername;
      $toUsername = $POSTOBJ->tousername;
      $keyword = Trim ($postObj->content);
      $time = time (); $MsgType = $POSTOBJ->msgtype; Add $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[%s]]></msgtype> <content><!
      [cdata[%s]]></content> <FuncFlag>0</FuncFlag> </xml> ";
          if ($MsgType!= ' event ') {if (!empty ($keyword)) {$msgType = "text";
        $CONTENTSTR = "Welcome to WeChat world!";
        }else{echo "Input something ...";
        } else {$msgType = "text"; $CONTENTSTR = "Thank you for your attention to Andyyang personal blog micro-letter assistant."
          \ r \ n ".
          "Reply" 1 "returns two newest articles \ r \ n".
          "Reply" 2 "return two popular article \ r \ n".
          "Reply" 3 "return two hot comment article \ r \ n".
          "Reply" 4 "returns two articles about the latest technology \ r \ n".
          "Reply" 5 "returns two new writing articles \ r \ n".
          "Reply to the other two articles that return search keywords \ r \ n". "More wonderful content, do in: <a href= ' http://www.jb51.net ' >www.jb51.net</a>.
        Parents, please support Oh, thank you ~ ";
      ;
      $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR); Echo $rESULTSTR;
      }else {echo "";
    Exit
    The Private Function checksignature () {$signature = $_get["signature"];
    $timestamp = $_get["timestamp"];
    $nonce = $_get["nonce"];
    $token = token;
    $TMPARR = Array ($token, $timestamp, $nonce); Sort ($TMPARR, sort_string);
    This adds a second parameter to the new SDK (compare items as strings) $tmpStr = implode ($TMPARR);
    $TMPSTR = SHA1 ($TMPSTR);
    if ($tmpStr = = $signature) {return true;
    }else{return false;

 }
  }
}

Of course, here is just simple implementation, in the micro-trust platform for the SDK to do a simple modification, actually msgtype type, even if the message type is event, it also has subscribe, location, and so on, and if the refinement, Use event as subscribe to handle the first-attention events, the code is as follows:

<?php define ("TOKEN", "Weixin");
$WECHATOBJ = new Wechatcallbackapitest ();
$WECHATOBJ->weixin_run ();
  Class Wechatcallbackapitest {private $fromUsername;
  Private $toUsername;
  Private $times;
  Private $keyword;
  Private $MsgType;
 Public Function responsemsg () {$postStr = $GLOBALS ["Http_raw_post_data"];
      if (!emptyempty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
      $this->fromusername = $postObj->fromusername;
      $this->tousername = $postObj->tousername;
      $this->keyword = Trim ($postObj->content);
      $this->time = time ();
    $this->msgtype = $postObj->msgtype; else {echo "Pay attention to <a href= ' http://{$_server[' http_host ']} ' >http://{$_server[' Http_host"]}&LT;/A&G
     T;,thanks! ";
    Exit
    The Public Function Weixin_run () {$this->responsemsg ();
if ($this->msgtype!= ' event ') {//attention $data = $this->getdata ();     $this->fun_xml ("News", $data, Count ($data));
     else {$data = $this->getweldata ();
    $this->fun_xml ("text", $data, 1); }//type:text text type, news type//text,array (content), array (ID)//news,array (Array (title, Introduction, Picture, HYPERLINK),... Less than 10), the number of private function Fun_xml ($type, $value _arr, $count) {$con = "<xml> <tousername><![ cdata[{$this->fromusername}]]></tousername> <fromusername><!
   [cdata[{$this->tousername}]]></fromusername> <createtime>{$this->times}</createtime> <msgtype><!
    [cdata[{$type}]]></msgtype> "; Switch ($type) {case "text": $con. = "<content><![
     cdata[$value _arr]]></content> ";
   Break
   Case "News": $con. = "<articlecount>{$count}</articlecount> <Articles>"; foreach ($value _arr as $key => $v) {$con. = "<item> <title><![ cdata[{$v [0]}]]></title> &LT;DESCRIPTION&Gt;<! [cdata[{$v [1]}]]></description> <picurl><! [cdata[{$v [2]}]]></picurl> <url><!
   [cdata[{$v [3]}]]></url> </item> ";
     $con. = "</Articles>";
   Break echo $con. "
 </xml> ";
    The Private Function GetData () {//database is queried by keyword//.....
    。。。。。。。。。。。。
  Returns an array of the results of the article return $data; Private Function Getweldata () {$data = "Thank you for your attention to Andyyang personal blog micro-letter assistant."
          \ r \ n ".
          "Reply" 1 "returns two newest articles \ r \ n".
          "Reply" 2 "return two popular article \ r \ n".
          "Reply" 3 "return two hot comment article \ r \ n".
          "Reply" 4 "returns two articles about the latest technology \ r \ n".
          "Reply" 5 "returns two new writing articles \ r \ n".
          "Reply to the other two articles that return search keywords \ r \ n". "More wonderful content, do in: <a href= ' http://www.jb51.net/' >www.jb51.net</a>.
        Parents, please support Oh, thank you ~ ";
    ;
  return $data;

 }
}

Honestly, I really want to get a service number. Play, the custom menu is no technical content, but the following micro-letter payment, service number only services, or quite worth trying.

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.