美文网首页
2018-06-27

2018-06-27

作者: 萧枫1 | 来源:发表于2018-06-27 17:57 被阅读0次

    php微信扫描登陆

    locate是一个框架的跳转方法,参考就行。

    <a href="index/wxlogin">微信登陆</a>

    /**

        * 微信登录

        */

    public function wxlogin(){

    $appid=你的appid;

    $url="https://xxxxx/index/wxredirecturi";//你接收微信返回数据的路径

    $state=asdadadasdaadada;//随机值

    $_SESSION['state']=$state;

    $wxurl="https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$url&response_type=code&scope=snsapi_login&state=$state#wechat_redirect";

    header($this->locate($wxurl));

    }

    public function wxredirecturi($code,$state){ //接收微信返回数据的方法

    if($code&&$state==$_SESSION['state']){

    unset($_SESSION['state']);

    $user=$this->getwxuserinfo($code); //获取到code之后就能获取到用户数据

    if($user=='1'){ //成功

    $this->locate(’url‘); //跳到你想要跳到的页面路径

    }elseif($user=='2'){ //成功

    $this->locate(’url‘); //跳到你想要跳到的页面路径

    }

    }else{//失败

    $this->locate(’url‘); //跳到你想要跳到的页面路径

    }

    }

    /**获取用户信息

        * @param $code

        */

    public function getwxuserinfo($code){

    $auth=$this->getwxaccestoken($code);

    $access_token=$auth[access_token];

    $openid=$auth[openid];

    $url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";

    $userinfo=$this-->curlget($url); //get请求

    return $userinfo;

    }

    /**

    * get请求

    * @param $url

    * @return mixed

    */

    public function curlget($url){

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );

        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );

        $response = curl_exec($ch);

        curl_close( $ch );

        $rest = json_decode($response,true);

        return $rest;

    }

    相关文章

      网友评论

          本文标题:2018-06-27

          本文链接:https://www.haomeiwen.com/subject/wfdiyftx.html