微信登录

 //微信登录
    function accept() {
        //这个链接是获取code的链接 链接会带上code参数
        $REDIRECT_URI = "http://bw.ten8.cn";
        echo $REDIRECT_URI . "<br>";
        $REDIRECT_URI = urlencode($REDIRECT_URI);
        echo $REDIRECT_URI . "<br>";
        $scope = "snsapi_userinfo";
        echo $scope . "<br>";
        $state = md5(mktime());
        echo $state . "<br>";
        
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . C(WEXIN_APPID) . "&redirect_uri=" . $REDIRECT_URI . "&response_type=code&scope=" . $scope . "&state=" . $state . "#wechat_redirect";
        header("location:$url");
    }


    //用户同意之后就获取code  通过获取code可以获取一切东西了  
    function getCode() {
        //获取accse_token
        $code = $_GET["code"];
        //echo $code;
        //echo "<br>";
        //用code获取access_yoken
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . C(WEXIN_APPID) . "&secret=" . C(WEXIN_APPSECRET) . "&code=" . $code . "&grant_type=authorization_code";
        //这里可以获取全部的东西  access_token openid scope
        $res = $this->https_request($url);
        $res = json_decode($res, true);
        $openid = $res["openid"];
        echo "<pre>";
        //print_r($res);
        //echo $openid;
        //echo "<br>";
        $access_token = $res["access_token"];
        //echo $access_token;
        //这里是获取用户信息
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
        $res = $this->https_request($url);
        $res = json_decode($res, true);
        //写入session
        print_r($res);
        //把用户的信息写入session 以备查用
        $weixn = $res["openid"];
       // $nickname = $res["nickname"];
        //$_SESSION["weixin"] = $weixin;
        session("user1_weixin",$weixin);
        header("location:http://www.icoco.xin/wxt_webhome/test.php");
    }


    function https_request($url, $data = null) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值