快乐每一天

自己选择的路,跪着也要走下去...

标签 公众号 下的文章

PHP检测是否关注公众号,亲测可用

/*检测是否关注公众号*/
    /*subscribe    用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。*/
    /*https://www.cnblogs.com/mracale/p/9318349.html*/
    public function checkisgz(){
        $request = request();
        $callback = $request->url(true);//微信服务器回调url,这里是本页url

        $appid = config('appid');
        $secret = config('secret');

//微信网页授权获取openid
        $web_url=$callback;

        if (!isset($_GET['code'])) {
            $redirect_uri=urlencode($web_url);
            $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
            header("location:$url");exit();
        }
        $code=trim($_GET['code']);
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
        $access=file_get_contents($url);
        $data=json_decode($access,true);
        $access_token=$data['access_token'];
        $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid=OPENID&lang=zh_CN';
        $user=file_get_contents($url);
        $arr=json_decode($user,true);
//获取用户的openid
        $openid=$arr['openid'];

        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        $access=file_get_contents($url);
        $access_arr=json_decode($access,true);
//非网页的access_token
        $access_token=$access_arr['access_token'];

        $url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $res=file_get_contents($url);
        $res=json_decode($res,true);
//        var_dump($res);
        return $res['subscribe'];
    }
阅读全文