网页获取微信授权并获得用户信息

/_______________________________________________________/

这个坑可谓是用了一整天的的时间来填, 网上也没有确切说明, 直接上代码吧
后台采用Thinkphp 5来写的, 处理可能没有完善好, 响应时间较长(10s左右)

/_______________________________________________________/

*使用过的工具: *
- 1.微信web开发者工具(因为只能微信端打开)
- 2. PHPstorm PHP编辑器
- 3.谷歌浏览器 (无限踩坑, 用来查资料的)
- 4.Postman 接口测试工具

好啦, 上代码
/-----------------------前端的接口地址-----------------------------/
[前端链接] "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=回调地址&response_type=code&scope=snsapi_userinfo&state=WULJ(签名, 随便写)"
/----------------------------------------------------------------------/

index.php
_________________________分割线__________________________

";
        //授权, 换取openid
        $i2 = $_GET['code'];
        $params =  array(
            'appid' => $this->appid,
            'secret' => $this->secret,
            'redirect_uri' => 'https://wx.wnm8.cn/H5/0002/Api/public/',
            'response_type' => 'code',
            'scope' => 'snsapi_userinfo',
            'state' => 'WULJ',
            //'js_code' => "'".$i2."'", // 小程序传来的ticket
            //'grant_type' => 'authorization_code',
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, "https://open.weixin.qq.com/connect/oauth2/authorize");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $output = curl_exec($ch);
        if (false === $output) {
            echo '错误 : ' . curl_error($ch);
            exit();
        }
        /*---------------------*/


        //$output = $output['access_token'];
        /*---------------------*/
        //换取公众号token
        $params =  array(
            'appid' => $this->appid,
            'secret' => $this->secret,
            'code' => $i2,
            'grant_type' => 'authorization_code' ,
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/sns/oauth2/access_token");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $gzhh = curl_exec($ch);

        /*--------------------*/
        $gzhh = json_decode($gzhh);
        $gzhh = (array)$gzhh;
        $gzhh = $gzhh['access_token'];
        //通过上面的信息取得用户信息

        $params =  array(
            'access_token' => $gzhh,
            'openid' => $this->secret,
            'lang' => 'zh_CN',
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/sns/userinfo");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $yhxx = curl_exec($ch);

    //因为获取到的是字符串, 因而转成数组
        $yhxx = json_decode($yhxx);
        $yhxx = (array)$yhxx;
        //打印获得的元数据到页面上
        dump($yhxx);

    }

}

你可能感兴趣的:(网页获取微信授权并获得用户信息)