Application example of PHP memcache in WeChat public platform

Source: Internet
Author: User
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 ""; }?>

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.