技术相关 · 2014年10月28日

如何判定用户是否关注微信公众号

if (empty($openid)){
        if (empty($_REQUEST["code"])) {
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$APPID."&redirect_uri=".$redirect_url."&response_type=code&scope=snsapi_base&state=blinq#wechat_redirect";
            echo $this->assign('url', $url)->fetch('redirect');
            return;
        }else{
            $code = $_REQUEST['code'];
        
            $APPID = C("APPID");
            $SECRET = C("SECRET");
            $accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $APPID . "&secret=" . $SECRET . "&code=$code&grant_type=authorization_code";
            $ch = curl_init($accessTokenUrl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $info = curl_exec($ch);
            $dataJson = json_decode($info, true);
            $openid = $dataJson['openid'];
            $userdata = $table->get_subscribe_res($openid);
            $subscribed = $userdata['data']['subscribed'];
            if($subscribed){
                session("openid", $openid);
            
            }else{
                $url="http://www.oschina.net/code/step1?catalog=";
                echo $this->assign('url', $url)->fetch('redirect');
                return;
            }
        }
    }