前提
先去微信开放平台申请 交完300块钱保护费后等个几天就通过了 会给你 appid 和 AppSecret;刚需

code2Session 微信_code2Session 微信


https://open.weixin.qq.com 微信开放平台

有了appid和AppSecret 开发就可以开始了

code2Session 微信_ci_02


https://open.weixin.qq.com/connect/qrconnect?appid=这里填写appid&redirect_uri=这里填写你的回调地址就是登录成功后需要跳回的地址不能带端口号和是外网可访问的网站&response_type=code&scope=这里如果是登录的话就填snsapi_login写&state=STATE#wechat_redirect



出现这个页面说明ok了然后扫描登录 页面会跳转到你填的回调页面上 看看地址栏有个code

code2Session 微信_ci_03

然后进入第二步 通过code获取access_token

https://api.weixin.qq.com/sns/oauth2/access_token?appid=这里填写你的appid&secret=这里填写你的appsecret&code=这个code是地址栏上的&grant_type=authorization_code

然后去浏览器访问得到这一串东西

code2Session 微信_code2Session 微信_04


需要 access_token openid第三步 用 access_token 换取用户资料

https://api.weixin.qq.com/sns/userinfo?access_token=这里填上刚获取的&openid=这个也是; 拿去浏览器一跑 完美

code2Session 微信_微信第三方登录_05


用户数据以到手

全部步骤和在一起就是这样写 直接拷贝复制就能用

/*
   * 微信一条龙服务 一般只需要改这个 $wxconfig 就行
   * */
public function wxoauth($code){
    $wxconfig=array(
        'app_id'=> '这是你的appid',
        'app_secret' =>'这是你的app_secret',
        'redirect_uri'=> '这是你的回调地址成功后跳转的'
    );

    //拼接url
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$wxconfig['app_id']."&secret=".$wxconfig['app_secret']."&code=".$code."&grant_type=authorization_code";

    //调用curl方法得到返回值
    $result = $this->http($url, 'get');

    //将json数据转换为数组,得到accesstoken等数据

    $arr = json_decode($result[1],true);
    //判断请求状态是否正确
    if ($result['0'] == '200' && empty($arr['errcode'])) {

        $userInfo = $this->wechat($arr['access_token'], $arr['openid'],$arr['refresh_token'],$wxconfig);
        return true;
    }else {
        //请求失败
        return false;
    }

}


/**
     * 微信获取用户数据
     * @param $token
     * @param $openid
     * @param $refresh_token
     * @return array
     */
    public function wechat($token, $openid, $refresh_token,$wxconfig)
    {
        //拼接获取用户信息的url
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid;
        //发送curl请求,获取返回数据
        $result = $this->http($url, 'get');


        //判断请求状态
        if ($result[0] == '200') {
            //将返回的数据整理成数组
            $arr = json_decode($result[1], true);

            //如果数组中含有键名为errcode,且值等于41001,则说明token过期
            if (array_key_exists('errcode', $arr) && $arr['errcode'] == 41001) {
                //调用刷新token方法,并返回
                return $this->refreshToken($refresh_token,$wxconfig);
            } else {
                //没有过期则返回用户的信息
                return $arr;
            }
        } else {
            //请求失败
            return false;
        }
    }

 /**
     * 刷新token
     * @param $refresh_token
     * @return bool|mixed
     */
    public function refreshToken($refresh_token,$wxconfig)
    {
        //获取配置文件的中的数据,没用过的没关系,就是获取配置文件中的配置

        $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $wxconfig['app_id'] . '&grant_type=refresh_token&refresh_token=' . $refresh_token;
        $result = $this->http($url, 'get');
        if ($result[0] == 200) {
            return json_decode($result[1], true);
        } else {
            return false;
        }
    }



    /**
     * @param $url 地址
     * @param $method 请求方式
     * @param null $postfields post的数据
     * @param array $headers 请求头
     * @param bool $debug 调试模式
     * @return array
     */
    public function http($url, $method, $postfields = null, $headers = array(), $debug = false)
    {
        $ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);

        switch ($method) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                    $this->postdata = $postfields;
                }
                break;
        }
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, true);

        $response = curl_exec($ci);
        $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

        if ($debug) {
            echo "=====post data======\r\n";
            var_dump($postfields);

            echo '=====info=====' . "\r\n";
            print_r(curl_getinfo($ci));

            echo '=====$response=====' . "\r\n";
            print_r($response);
        }
        curl_close($ci);
        return array($http_code, $response);
    }