This article mainly introduces the application method of PHP memcache on the WeChat public platform, and analyzes the interactive mode implementation skills of PHP using memcache to save user access records and respond to user access and give prompt information based on specific examples. Friends in need You can refer to the following example
This article describes the application method of PHP memcache on the WeChat public platform. Share it with everyone for your reference, the details are as follows:
Now most interactions on the WeChat public platform are users sending messages -> WeChat analyzes and returns the results. This mode has a relatively single function. Here is another interactive mode: the user sends information ->WeChat analyzes the information and prompts the next step ->the user sends information->....->WeChat returns the results. This article introduces the use of memcache in sae to implement the above mode.
Idea:
1. When the user sends a message, WeChat will record the user’s unique openid and assign its value to $fromUsername.
2. Use memcache to save two data, one is the last message sent by the user, and the other is a randomly defined value (1 in the example) for judging user actions.
3. Use $fromUsername as key positioning.
The sample code is as follows:
FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = ""; //定义变量last_step,记录上一次动作 $last_step=$mc->get($fromUsername."step"); //定义变量last_data,记录上一次数据 $last_data=$mc->get($fromUsername."data"); if(!empty( $keyword )){ //判断用户动作 if($last_step!=1){ //将用户输入的数据保存至memcache $mc->set($fromUsername."data",$keyword,0,120); //记录用户这一次动作,值设为1 $mc->set($fromUsername."step",1,0,120); $msgType = "text"; $contentStr = "请再输入一个值:"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } else { //清空memcach动作 $mc->delete($fromUsername."step"); //清空memcach数据 $mc->delete($fromUsername."data"); $msgType = "text"; $contentStr = "你输入的第一个值为:".$last_data."\n 第二个值为:".$keyword; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } } } else { echo ""; } ?> %s 0
The above is the detailed content of Application examples of PHP memcache on WeChat public platform. For more information, please follow other related articles on the PHP Chinese website!