PHP判断公众号是否关注并跳转关注页

php代码(Laravel框架):

use GuzzleHttp\Client;
   public function is_follow(Request $request)
    {
        $user_info=DB::table('sdb_user')->where('id',$this->user['id'])->value('openid');
        $data=DB::table('sdb_config')->where('name','token')->value('value');
        $data=json_decode($data);
        //判断是否需要重新获取token
        if ($data->expire_time < time()){//到期
            $appId=env('WECHAT_APP_ID');
            $appSecret=env('WECHAT_APP_SECRET');
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
            $res1=$this->httpGet($url);
            $res = json_decode($res1);
            $res_time=[
                'token'=>$res->access_token,
                'expire_time'=>time()+7000,
            ];
            $res_time=json_encode($res_time);
            DB::table('sdb_config')->where('name','token')->update(['value'=>$res_time]);
            $access_token = $res->access_token;
        }else{
            $access_token=$data->token;
        }
        //获取用户是否关注公众号
        $client1 = new Client();
        $client1 = $client1->request('get', "https://api.weixin.qq.com/cgi-bin/user/info", [
            'query' => [//get方式
                //'access_token' => $pageContents['access_token'],
                'access_token' => $access_token,
                'openid' => $user_info,
                'lang' => 'zh_CN',
            ],
        ]);
        $res = $client1->getBody();

$res = json_decode($res, true);
        return format($res['subscribe'],'是否关注');
        //"subscribe"

    }
    function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

 

前端代码:

is_follow() {
                uni.request({
                    url: this.$site_url + 'index/is_follow',
                    method: 'GET',
                    header:{
                        "Authorization":"Bearer "+ uni.getStorageSync("token")
                    },
                    success: res => {
                        if(res.data.code == 0){
                            window.location.href = "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzg5NDMzODg2NQ==&scene=110#wechat_redirect";
                        }
                    },
                })
            },

如果没有关注直接跳关注页。

注:如何快速获取biz

第一步:点击公众号右上角然后弹出公众号标识,然后点击查看历史消息

第二步:在弹出的窗口中选择复制链接地址

然后我们复制一下就可以看到对应的BIZ了

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值