Home > Backend Development > PHP Tutorial > Application examples of PHP memcache on WeChat public platform

Application examples of PHP memcache on WeChat public platform

巴扎黑
Release: 2023-03-16 07:42:02
Original
1455 people have browsed it

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:


<?php
/**
 * wechat php test
 */
//调用mecache
$mc=memcache_init();
//接受消息,直接用微信官方的示例代码
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
   if (!empty($postStr)){
   $postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, 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>";
   //定义变量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 "";
  }
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template