微信公众号方法(不定时更新)

1.判断用户是否关注(其实这一步也拉取了用户信息,不过项目需要单独实时查询,所以单独写了)

    public function isAttention($openid){
        $model = new HomeConfWeixin();
        $access_token = $model->getAccesstoken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $result = $model->http_get($url);
        $result = json_decode($result);
        return $result->subscribe;
    }

2.获取accessToken

    //获取accesstoken
    public function getAccesstoken()
    {
        $conf=M("jx_wxconf");
        $appid=$conf->where(array("varname"=>"appid"))->getField('value');
        $secret=$conf->where(array("varname"=>"appsecret"))->getField('value');
        $expires_in=$conf->where(array("varname"=>"expires_in"))->getField('value');
        $oaccess_token=$conf->where(array("varname"=>"access_token"))->getField('value');

        if ($expires_in < time() or ! $expires_in)
        {
            $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
            $info=$this->curl_post($url,$post_data='');
            $dataJson = json_decode($info, true);
            if ($dataJson) {
                $access_token = $dataJson['access_token'];
                $data['value'] = time() + 3600; //保存1小时
                M("jx_wxconf")->where(array("varname"=>"expires_in"))->save($data);
                $d['value'] = $dataJson['access_token'];
                M("jx_wxconf")->where(array("varname"=>"access_token"))->save($d);
            }
        } else {
            $access_token = $oaccess_token;
        }
        return $access_token;
    }
技术最菜,头发最少
原文地址:https://www.cnblogs.com/gushengyan/p/9635213.html