zoukankan      html  css  js  c++  java
  • tp5 微信授权

    protected $appid = '****************';         //微信 appid
    protected $appsecrt = '******************';     //微信 appsecrt
    
    
    //-----------静默授权   (不能获取用户的昵称、头像,要获取用户的昵称和头像使用  用户确认授权)
    public function getBaseInfo()
    {
        //获取code
        $redirect_uri = urlencode("http://www.******.com/index/index/getWxCode");//回调地址
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; //state 可任意
        $this->redirect($url, 302);// tp5的重定向方法 
    }
    
    public function getWxCode()
    {
        //获取access_token
        //获取openID
        $code = $_GET['code'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecrt . "&code=$code&grant_type=authorization_code";
        $r = http_curl($url);
        $openid = $r['openid'];
        $access_token = $r['access_token'];
        var_dump('access_token是:' . $access_token . '=========' . 'openid是:' . $openid);
    }
    
    
    //-------------------------------end-----------------------------------
    
    
    //-----------用户确认授权          
    public function getCodeUserInfo($temp)
    {
        //获取code
        $redirect_uri = urlencode("http://zs.zs13ce.gx.cn/index/index/getWxUserInfo");//回调地址
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=$temp&connect_redirect=1#wechat_redirect"; //state 可任意
        $this->redirect($url, 302);// tp5的重定向方法 
    }
    
    
    public function getWxUserInfo()
        {
            //换取网页授权access_token
            //获取openid
            $code = $this->request->param('code');
            $state = $this->request->param('state');
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecrt . "&code=" . $code . "&grant_type=authorization_code";
            $rjson = http_curl($url);
            $access_token = $rjson['access_token'];//得到access_token ( 网页的)
            $openId = $rjson['openid'];//得到openid
            $userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=" . $this->appid . "&lang=zh_CN";//获取用户信息
            $result = http_curl($userInfoUrl);
    //        var_dump($result);//打印用户详细信息
    //        die;
            if (empty($result)) {
                echo 0;die;
            }
            $user = Db::name("user")->where("openid", $result["openid"])->find();
            if (empty($user)) {
                //如果表里没有,则插入
            }
            $this->_user = $user;
            $this->setIsSq($user);
            $this->redirect(url($state), 302);// tp5的重定向方法 
        }
    
    
    
    
    function http_curl($url, $data=[])
    {
        $curl = curl_init();//初始化
        curl_setopt($curl, CURLOPT_URL, $url);//设置抓取的url
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// https请求 不验证证书和hosts
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HEADER, 0);//  设置头文件的信息作为数据流输出  设置为1 的时候,会把HTTP信息打印出来  不要http header 加快效率
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出。 如果设置是0,打印信息就是true
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data,true));
        $data = curl_exec($curl);//执行命令
        $result = json_decode($data, true);
        if ($data == false) {
            echo "Curl Error:" . curl_error($curl);
            exit();
        }
        curl_close($curl);//关闭URL请求
        return $result;
    }
  • 相关阅读:
    ASP.NET页面事件执行过程 总结
    程序员最应该读的图书(中译版) [收藏]
    C# 中的委托和事件的详解资料
    已添加项。字典中的关键字
    TFS 删除团队项目集合
    注册后第一篇
    类型的权限已失败 SqlClientPermission
    C#创建Oracle存储过程
    使用MySQL with 递归查询菜单树
    MySQL 常用TSQL(持续更新...)
  • 原文地址:https://www.cnblogs.com/j-jian/p/11908997.html
Copyright © 2011-2022 走看看