PHP version of the micro-credit development of receiving messages, automatically judge and reply to the corresponding message method _php instance

Source: Internet
Author: User
Tags cdata sprintf

This article describes the PHP version of the micro-credit development of the receiving message, automatic judgement and reply to the corresponding message method. Share to everyone for your reference, specific as follows:

The micro-mail API has now been powerful enough to be able to call our website or the content of the designated database to send back the message, here to introduce a micro-letter development notes.

Send 1,2,3,7 to test the corresponding message, Menu mode needs to be upgraded to a service number, or the number of subscribers more than 500.

1. The message sent by the user is received through the URL of the developer you submitted, where the subscription number can be done, sent over are all XML format data, need to parse, the code is as follows:

$data = $GLOBALS ["Http_raw_post_data"];
if (!emptyempty ($data)) {//Receive message and process
$xml = (array) simplexml_load_string ($data, ' simplexmlelement ', libxml_nocdata );
return $xml;
}

Replies are also data formats that need to return XML.

2. Take the initiative to send the user message, query the user information, create a custom menu (now the subscription number can also be used), as well as some advanced features, are required to obtain AppID and Appsecret in order to get access_token.

Access_token this thing, in addition to receiving information, each interface to use, Access_token time of 7200 milliseconds expired need to be retrieved.

For more information, please refer to: http://mp.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5.

At present, I have implemented, receive messages, automatically judge reply to the corresponding message news, create a custom menu, click the Custom menu to do the appropriate action, the user list, and so on, feeling that the current development mode and subscription mode is no different.

Here is a test example of a test response, the following code:

<?php/** * WeChat PHP Test * *//define Your token ("define", "token on your Application");
$WECHATOBJ = new Wechatcallbackapitest ();
$WECHATOBJ->valid ();
    Class Wechatcallbackapitest {public function valid () {$echoStr = $_get["Echostr"];
     Valid signature, option if ($this->checksignature ()) {$this->responsemsg ();
    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 ();
        $msgType = "text"; $TEXTTPL = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> &Lt Createtime>%s</createtime> <msgtype><! [cdata[%s]]></msgtype> <content><!
  [cdata[%s]]></content> <FuncFlag>0</FuncFlag> </xml> ";
   if (!emptyempty ($keyword)) {$contentStr = $this->keyrep ($keyword);
   if (Emptyempty ($CONTENTSTR)) {$contentStr = "You are intentional, no culture is really terrible";//You are intentional, no culture really terrible;
         }//$contentStr = @iconv (' UTF-8 ', ' gb2312 ', $keyword);
         $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
        Echo $resultStr; }else{$contentStr = ' No culture really terrible, incredibly can't type!
         ';//$this->keyrep ($keyword);
   $CONTENTSTR = @iconv (' UTF-8 ', ' gb2312 ', $keyword);
         $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);
 $TMPSTR = implode ($TMPARR);
 $TMPSTR = SHA1 ($TMPSTR);
 if ($tmpStr = = $signature) {return true;
 }else{return false;
 } function Keyrep ($key) {//return $key;
  if ($key = = ' Hi ' | | $key = ' in ' | | $key = = ' Hello ') {$mt = Mt_rand (1,17); $array = Array (1=> ' suicide, later) ... ',2=> ' Something for me please shout! ',3=> ' I was running naked, had rushed out of the service area ',4=> ' I now position: WC; posture: squat; face: twitch; state: Force ... ',5=> ' to eat, if you are handsome, please contact me, if you are a beautiful woman ..... Even if you are a beautiful woman, I have to eat first, ",6=> ' bath ~ ~ declined to watch!! ^_^ 0 ',7=> ' have bears out?] I'm going to snare and come back as soon as possible. ',8=> ' Hello, I am 500, would you be 250? ',9=> ' Hello! Garbled ah, again send ',10=> ' not I ignore you, but time is hard to resist! ',11=> ' What you just said, I didn't see clearly, please say it again! ',12=> ' hair more than a few times ah ~ ~ More than a few times I will return to you. ',13=> ' This man has died, something burning! ',14=> ' good, not anxious oh ... ',15=> ' hello. I'm going to kill a couple of people and come back soon. ',16=> ' I have become immortal? Buddha said that there is smoke, no fire can not be a fairy. ',17=> ' you want to talk to me? Are you really going to talk to me? Are you sure you want to say something? Must you say it? Then you say, this is the automatic reply, anyway I can't see in fact I am in ~ just don't return you to take me how?
  '
);
 return $array [$MT];
  if ($key = = ' by ' | | $key = ' AH ' | | $key = = ') {$mt = Mt_rand (1,19); $aRray = Array (1=> ' People's first sexual goodness? ',2=> ' The way forward? I hope you can go down on your own? And I'm riding the ',3=> ' joke what is it? That's what I'm saying to you right now. ',4=> ' Everybody says I'm ugly? In fact, I'm just not obvious. ',5=> ' A; how did the pig die? B; you're not dead. How do I know that ',6=> ' Obama has killed two people with his namesake? Altman, you have to be careful. ',7=> ' Some people alive? He's dead. Some people are alive? ',8=> ', sister, you're on a boat? Brother, I'm walking ashore. "It is said that very stupid people see all are singing out." ',9=> ' I've only got two things in my life? That's not going to happen. ',10=> ' past this village? That's because there's a store. ',11=> ' I thought you were just a ball? Didn't think? You're a real ball. ',12=> ' You finally come, I've been looking for you for n years, what did you do to Mars? I'm going to Pluto and I'll talk to you later, don't walk away ',13=> ' you have the right to remain silent, everything you say will be recorded as a record. You can ask for a proxy server if the network will not be available for you to assign one. ',14=> ' I am being wanted by Interpol worldwide, if you have the message of this person, please call the local alarm phone ',15=> ' bath ~ declined to sit on the sidelines!! ^_^ 0 ',16=> ', here is the mobile Secretary, Meimei please send again, I will contact you, sister please send another two times, I will contact you, brother, brother will not send again, because hair also don't contact you! ',17=> ' In fact I am in ~ just don't return you to take me how? ',18=> ' What you just said, I didn't see clearly, please say it again! ',19=> ' well-behaved, not anxious ...
  ');
 return $array [$MT];
  if ($key = = ' Ask ') {$mt = Mt_rand (1,5); $array = Array (1=> ' My face oil??) I do not know ',2=> ' go, I invite you to eat ',3=> ' This person has died, something burning! ',4=> ' Hello! What the hell! Garbled ah, again hair ',5=> ' joke is what?? is what I'm saying to you right now.
  ');
 return $array [$MT];
 Return ""; function Keylist () {$array = array (1=> ' Hi ',2=> ' hello ',3=> ' by ',4=> ')5=> ' ask ');

 }}?>

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.