This article mainly introduces the application method of PHP Memcache in public platform, combined with concrete instance form analysis PHP use Memcache to save user access record and respond to user access and give hints information interactive mode implementation skills, the need for friends can refer to the next
This paper describes the application of PHP memcache in the public platform. Share to everyone for your reference, as follows:
Most of the interaction of the public platform is now the user sends the information--analysis and returns the result, this mode function is relatively single. Here is another interactive mode: Users send information---analysis information and prompt the next action, users send information->.......-> return results. This paper introduces the use of Memcache in SAE to achieve the above pattern.
Ideas:
1, the user sends the message, will record the user's unique OpenID, assigns its value to the $fromusername.
2. Save two data with Memcache, one for the last message sent by the user, and the other to arbitrarily define a value (1 in the example) to determine the user's action.
3, using $fromusername as key positioning.
The sample code is as follows:
<?php/** * WeChat PHP test *///call Mecache$mc=memcache_init ();//accept the message directly with the official sample code $POSTSTR = $GLOBALS ["Http_raw_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 (); $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> "; Define the variable Last_step, record the previous action $last _step= $MC->get ($fromUsername. " Step "); Define the variable last_data, record the last data $last _data= $MC->get ($fromUsername. " Data "); if (!empty ($keyword)) {//Determine user action if ($last _step!=1) {//number of user inputIt was saved to Memcache $MC->set ($fromUsername. " Data ", $keyword, 0,120); Record the user's action with the value set to 1 $MC->set ($fromUsername. " Step ", 1,0,120); $msgType = "text"; $CONTENTSTR = "Please enter a value again:"; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR); Echo $resultStr; } else {//empties the Memcach action $MC->delete ($fromUsername. " Step "); Empty the Memcach data $mc->delete ($fromUsername. Data "); $msgType = "text"; $CONTENTSTR = "The first value you entered is:". $last _data. " \ n The second value is: ". $keyword; $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR); Echo $resultStr; }}} else {echo ""; }?>