Micro-Credit development notes to receive messages, automatically determine the reply to the corresponding message

Source: Internet
Author: User
Tags cdata sprintf


Bobcat's blog, 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 is more than 500.

1. The messages sent by the user are received through the developer URL you submitted. The subscription number can be done here.

All the data sent is XML format, which needs to be resolved:

The code is as follows Copy Code

$data = $GLOBALS ["Http_raw_post_data"];
if (!empty ($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 needs to be used. The Access_token time is 7200 milliseconds.

Expiration requires a new fetch.

More please refer to: http://mp.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5 amount

At present I have implemented, receive the message, automatically judge reply to the corresponding message (news), create a custom menu, click on the custom menu to do the appropriate action. User list. Feel the current development mode and subscription mode is no different ...

Here's a test example of a test response

The code is as follows Copy Code

<?php
/**
* WeChat PHP Test
*/

Define your Token
Define ("TOKEN", "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
}
}

Public Function responsemsg ()
{
Get post data, May is due to the different environments
$POSTSTR = $GLOBALS ["Http_raw_post_data"];

Extract Post Data


if (!empty ($POSTSTR)) {





$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);


$fromUsername = $POSTOBJ-&gt;fromusername;


$toUsername = $POSTOBJ-&gt;tousername;


$keyword = Trim ($postObj-&gt;content);


$time = time ();


$msgType = "text";


$TEXTTPL = "&lt;xml&gt;


&lt;tousername&gt;&lt;! [cdata[%s]]&gt;&lt;/tousername&gt;


&lt;fromusername&gt;&lt;! [cdata[%s]]&gt;&lt;/fromusername&gt;


&lt;CreateTime&gt;%s&lt;/CreateTime&gt;


&lt;msgtype&gt;&lt;! [cdata[%s]]&gt;&lt;/msgtype&gt;


&lt;content&gt;&lt;! [cdata[%s]]&gt;&lt;/content&gt;


&lt;FuncFlag&gt;0&lt;/FuncFlag&gt;


&lt;/xml&gt; ";


if (!empty ($keyword))


{





$CONTENTSTR = $this-&gt;keyrep ($keyword);


if (empty ($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-&gt;keyrep ($keyword);


$CONTENTSTR = @iconv (' UTF-8 ', ' gb2312 ', $keyword);


$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);


Echo $resultStr;


}

}else {


echo "";


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;


}


}





function Keyrep ($key)


{


return $key;


if ($key = = ' Hi ' | | $key = ' in? ' | | $key = ' Hello ') {


$MT = Mt_rand (1,17);


$array = Array (1=&gt; ' suicide, later) ... ',2=&gt; ' Something for me please shout! ',3=&gt; ' I was running naked, had rushed out of the service area ',4=&gt; ' I now position: WC; posture: squat; face: twitch; state: Force ... ',5=&gt; ' to eat, if you are handsome, please contact me, if you are a beautiful woman ..... Even if you are beautiful, I have to eat first, ',6=&gt; '


In the bath ~ declined to sit on the sidelines!! ^_^ 0 ',7=&gt; ' have bears out?] I'm going to snare and come back as soon as possible. ',8=&gt; ' Hello, I am 500, would you be 250? ',9=&gt; ' Hello! Garbled ah, again send ',10=&gt; '


It's not that I ignore you, but time is hard to resist! ',11=&gt; ' What you just said, I didn't see clearly, please say it again! ',12=&gt; ' hair more than a few times ah ~ ~ More than a few times I will return to you. ',13=&gt; ' This man has died, something burning! ',14=&gt; ' good, not anxious oh ... ',15=&gt; ' hello. I'm going to kill a couple of people and come back soon. ',16=&gt; ' I have become immortal? Buddha said that there is smoke, no fire can not be a fairy. ',17=&gt; '


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=&gt; ' The beginning of the person) sex is good? ',2=&gt; ' The way forward? I hope you can go down on your own? And I'm riding the ',3=&gt; ' joke what is it? That's what I'm saying to you right now. ',4=&gt; ' Everybody says I'm ugly? In fact, I'm just not obvious. ',5=&gt; ' A; how did the pig die? B; How do I know ',6=&gt; ' when you're not dead?


Obama has killed two people with his namesake? Altman, you have to be careful. ',7=&gt; ' Some people alive? He's dead. Some people are alive? ',8=&gt; ', sister, you're on a boat? Brother, I'm walking ashore. "It is said that very stupid people see all are singing out." ',9=&gt; ' I've only got two things in my life? That's not going to happen. ',10=&gt; '


Past this village? There's no store. That's because there's a branch. ',11=&gt; ' I thought you were just a ball? Didn't think? You're a real ball. ',12=&gt; ' 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=&gt; ' 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=&gt; ' I am being wanted by Interpol worldwide, if you have the message of this person, please call the local alarm phone ',15=&gt; ' bath ~ declined to sit on the sidelines!! ^_^ 0 ',16=&gt; ', 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=&gt; '


In fact, I am in ~ just don't return what do you take me? ',18=&gt; ' What you just said, I didn't see clearly, please say it again! ',19=&gt; ' well-behaved, not anxious ... ');


return $array [$MT];


}





if ($key = = ' Excuse me ')


{


$MT = Mt_rand (1,5);


$array = Array (1=&gt; ' My face oil??) I do not know ',2=&gt; ' go, I invite you to eat ',3=&gt; ' This person has died, something burning! ',4=&gt; ' Hello! What the hell! Garbled ah, again hair ',5=&gt; ' joke is what?? is what I'm saying to you right now. ');


return $array [$MT];


}





Return "";





}





function Keylist ()


{


$array = Array (1=&gt; ' Hi ',2=&gt; ' hello ',3=&gt; ' by ',4=&gt; ' in ',5=&gt; ' ask ');


}


}

?>

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.