微信扫码登陆(PC)

前言:

首先须要申请,必须企业,个体工商户,媒体等申请地址  https://open.weixin.qq.com/


微信登陆类

class WechatLogin {

    private $app_id = '申请到的APPID'; //appid

    private $app_secret = '申请到的SECRET'; //secret

    private $redirect_uri = '负责接收的地址'; //回调地址


  static public $instance;

    static public function getinstance(){

        if(!self::$instance) self::$instance = new self();

        return self::$instance;

}


    /**

    * 获取微信授权链接

    *

    * @param string $redirect_uri 跳转地址

    * @param mixed $state 随机串防CSRF攻击

    */

    public function get_authorize_url($state)

{

        //编码URL字符串

        $redirect_uri = urlencode($this->redirect_uri);

        return  "https://open.weixin.qq.com/connect/qrconnect?appid={$this->app_id}&redirect_uri=              {$redirect_uri}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";

  }


    /**

    * 获取授权token

*

    * @param string $code 通过get_authorize_url获取到的code

*/

    public function get_access_token($code)

{

        $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code=      {$code}&grant_type=authorization_code";

        $token_data = $this->http($token_url);

        if($token_data[0] == 200)

{

            return json_decode($token_data[1], TRUE);

}

        return FALSE;

}

    /**

    * 获取授权后的微信用户信息

    *

    * @param string $access_token

    * @param string $open_id

    */

    public function get_user_info($access_token,$open_id)

{

        if($access_token && $open_id)

{

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

            $info_data = $this->http($info_url);

            if($info_data[0] == 200)

{

                return json_decode($info_data[1], TRUE);

  }

  }

        return FALSE;

  }

  public function http($url, $method = 'GET', $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);

      }

}


业务逻辑案例:


1:登陆

微信扫码登陆(PC)_第1张图片

--------wx_state:放CSRF攻击,保存在session中


2:回调处理


微信扫码登陆(PC)_第2张图片

你可能感兴趣的:(微信扫码登陆(PC))