最新公告
  • 欢迎您光临 我爱模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境! 立即加入钻石VIP
  • 微信遮罩php代码怎么用

    正文概述 管理员   2025-09-05   4

    微信遮罩是指在微信公众号中,让文章的一部分内容只对已关注公众号的用户可见,未关注公众号的用户看到的是遮罩提示。为了实现这个功能,需要在公众号后台添加相应的事件和代码。

    以下是实现微信遮罩的PHP代码:

    ```php

    <?php

    // 验证token

    $we***Obj = new we***Callbackapi();

    if (isset($_REQUEST['echostr'])) {

    $we***Obj->valid();

    } else {

    // 第一步:获取用户关注状态

    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

    if (!$postStr) {

    echo '';

    exit;

    }

    // 第二步:获取用户openid

    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

    $fromUsername = $postObj->FromUserName;

    $toUsername = $postObj->ToUserName;

    $keyword = trim($postObj->Content);

    $msgType = trim($postObj->MsgType);

    $time = time();

    // 第三步:处理用户消息事件

    if ($msgType == 'event') {

    $event = $postObj->Event;

    if ($event == 'subscribe') {

    // 关注事件

    $contentStr = '欢迎关注我的公众号!请回复"你想看的内容",即可获得权限,查看内容。';

    $msgType = 'text';

    $resultStr = $we***Obj->responseMsg($fromUsername, $toUsername, $time, $msgType, $contentStr);

    echo $resultStr;

    }

    } else {

    // 第四步: 处理普通消息事件

    if ($keyword == '你想看的内容') {

    // 第五步: 判断用户是否关注公众号

    $url = "https://api.weixin.qq.com/cgi-bin/user/info?openid={$fromUsername}&access_token=";// 获取用户基本信息

    $access_token = $we***Obj->get_access_token();// 获取access_token

    $url = $url . $access_token;

    $res = $we***Obj->http_curl($url, 'get');// get请求

    $subscribe_info = json_decode($res, true);// 将json数据转换成数组

    $subscribe = isset($subscribe_info['subscribe']) ? $subscribe_info['subscribe'] : 0;

    if (!$subscribe) {

    // 用户未关注

    $msgType = 'text';

    $contentStr = '抱歉,您需要关注公众号才能查看内容。';

    $resultStr = $we***Obj->responseMsg($fromUsername, $toUsername, $time, $msgType, $contentStr);

    echo $resultStr;

    } else {

    // 用户已关注

    $msgType = 'text';

    $contentStr = '恭喜您,您已获得查看权限!以下是您想看的内容......';

    $resultStr = $we***Obj->responseMsg($fromUsername, $toUsername, $time, $msgType, $contentStr);

    echo $resultStr;

    }

    } else {

    $msgType = 'text';

    $contentStr = '欢迎关注我的公众号!请回复"你想看的内容",即可获得权限,查看内容。';

    $resultStr = $we***Obj->responseMsg($fromUsername, $toUsername, $time, $msgType, $contentStr);

    echo $resultStr;

    }

    }

    exit;

    }

    // 微信回调api

    class we***Callbackapi

    {

    public function valid()

    {

    // 原样返回echostr参数

    $echoStr = $_REQUEST["echostr"];

    if ($this->checkSignature()) {

    echo $echoStr;

    exit;

    }

    }

    // 验证签名

    private function checkSignature()

    {

    $signature = $_REQUEST["signature"];

    $timestamp = $_REQUEST["timestamp"];

    $nonce = $_REQUEST["nonce"];

    $token = 'your_token';

    $tmpArr = array($token, $timestamp, $nonce);

    sort($tmpArr, SORT_STRING);

    $tmpStr = implode($tmpArr);

    $tmpStr = sha1($tmpStr);

    if ($tmpStr == $signature) {

    return true;

    } else {

    return false;

    }

    }

    // 回应消息

    public function responseMsg($fromUsername, $toUsername, $time, $msgType, $contentStr)

    {

    $textTpl = "<xml>

    <ToUserName><![CDATA[%s]]></ToUserName>

    <FromUserName><![CDATA[%s]]></FromUserName>

    <CreateTime>%s</CreateTime>

    <MsgType><![CDATA[%s]]></MsgType>

    <Content><![CDATA[%s]]></Content>

    </xml>";

    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

    return $resultStr;

    }

    // 获取access_token

    public function get_access_token()

    {

    $redis = new Redis();

    $redis->connect('127.0.0.1', 6379);// 本机redis服务

    $redis->auth('your_redis_password');// 密码验证

    if ($redis->exists('access_token')) {// 如果缓存中存在access_token,返回缓存数据

    $access_token = $redis->get('access_token');

    } else {// 如果缓存中不存在access_token,请求微信API

    $appid = 'your_appid';// 公众号appid

    $appsecret = 'your_appsecret';// 公众号appsecret

    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";

    $res = $this->http_curl($url, 'get');

    $res = json_decode($res, true);

    $access_token = $res['access_token'];

    $redis->set('access_token', $access_token, 7000);// 缓存access_token,有效期7000秒

    }

    return $access_token;

    }

    // http请求封装方法

    public function http_curl($url, $method, $post_data = null)

    {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    if ($method == "post") {

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

    }

    $output = curl_exec($ch);

    curl_close($ch);

    return $output;

    }

    }

    需要替换代码中的三个值:

    - $token为你的公众号Token值

    - $appid为你的公众号appid

    - $appsecret为你的公众号appsecret

    添加完代码后,在公众号后台设置自定义菜单或者关键词回复,即可实现微信遮罩功能。

    微信遮罩效果是指在微信公众号内部分内容需要用户进行登陆 或者授权才可以查看,常常用于保护敏感信息,这个功能经常在很多微信公众号中使用。

    下面,我们将介绍如何利用php代码实现微信遮罩效果,具体的实现步骤如下:

    1. 首先需要获取用户的微信授权信息,可以通过OAuth2.0方式进行授权。这个过程需要公众号开发者获取微信提供的授权码,将授权码换取access_token和openid。

    2. 登录之后,需要判断用户的openid,如果用户openid存在就认为用户已经完成授权,可以查看内容,否则需要引导用户进行授权。

    3. 如果没有授权,我们需要显示一个提示弹窗,引导用户进行授权。可以使用微信提供的授权接口进行引导,将弹窗放在”我”页或引导页上。

    4. 登录之后,我们需要将用户的登录信息存储到session中,并刷新页面。

    5. 在页面中需要进行判断,如果用户已经完成了授权,则可以显示具体的内容,否则显示遮罩层。

    6. 在php代码中实现遮罩层,可以使用CSS来实现,具体的实现会使用到position、z-index、background-color等元素和属性。主要实现的原理就是通过设置深色的透明层来遮罩原页面,使得无法看到原页面的内容。

    7. 遮罩层实现的基本代码如下:

    <div id="mask_layer" style="display:none;left:0; top:0;position:fixed!important; position:absolute;top:0;left:0;z-index:99998;filter:alpha(opacity=60);-moz-opacity:0.6;-khtml-opacity: 0.6;opacity: 0.6;background-color: #000;width:100%;height:100%;text-align:center;">

    <div style="width: 200px;height: 150px;margin: 0 auto;margin-top:200px;background: #fff;border-radius: 10px;border: 1px solid #ccc;padding:20px;">

    <h3>温馨提示</h3>

    <p>请授权后查看后面的内容

    <a href="授权地址" class="btn">立即授权</a>

    </div>

    </div>

    在具体的引用页面中,需要根据授权状态动态地决定是否展示遮罩层,以下是实现代码:

    <?php

    session_start();

    if (isset($_SESSION["openid"])) {

    // 显示具体内容

    } else {

    // 显示遮罩层

    ?>

    <div id="mask_layer" style="display:block;">

    <h3>温馨提示</h3>

    <p>请授权后查看后面的内容

    <a href="授权地址" class="btn">立即授权</a>

    </div>

    <?php } ?>

    以上就是php实现微信遮罩效果的基本流程和代码实现,希望对你有所帮助。


    我爱模板网 » 微信遮罩php代码怎么用

    发表评论

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者
    script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?587cc1e5c052b5b0ce99533beff13c96"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();

    请选择支付方式

    ×
    支付宝支付
    余额支付
    ×
    微信扫码支付 0 元