第三方微信登陆

2019-05-16  本文已影响0人  七百年前
<?php

namespace App\Tools;

session_start();

/**
 * 微信授权相关接口
 *
 * @link http://www.phpddt.com
 */
class Thirdwxlogin
{

    //高级功能-》开发者模式-》获取
    private $app_id       = '11'; //公众号appid
    private $app_secret   = '22'; //公众号app_secret 8fc0236b1fbddb0208d8487c97db1ceb
    private $redirect_uri = '/wxlogin'; //授权之后跳转地址

    /**
     * 获取微信授权链接
     *
     * @param string $redirect_uri 跳转地址
     * @param mixed $state 参数
     */

    public function get_authorize_url($state)
    {
        $redirect_uri = urlencode($this->redirect_uri);
        $login_url    = "https://open.weixin.qq.com/connect/qrconnect?appid={$this->app_id}&response_type=code&redirect_uri={$redirect_uri}&forcelogin=true&scope=snsapi_login&state={$state}&lang=zh_CN#wechat_redirect";
        header("Location:$login_url");
    }

    /**
     * 获取授权token
     * array(6) {
      ["access_token"]=>
      string(88) "8_yX98o_i2qDE9oNHFCLRCylrVLpLWhqTZJPDY6XIPlPjBvCyhKtA1HnyJFW03njMfCeOCM_VOzu5iIvSOKC7QpQ"
      ["expires_in"]=>
      int(7200)
      ["refresh_token"]=>
      string(88) "8_HKI3hkInKKonEJ3teW_aBvq7K2240rs6oI2aWGRCOVKazipV5Y1qoiu3fXszce4KY6EGFBoBEJWLnKdrle-ALA"
      ["openid"]=>
      string(28) "o9Ns61WqAyXnc8fZV4hUHOtPrCYs"
      ["scope"]=>
      string(12) "snsapi_login"
      ["unionid"]=>
      string(28) "o1WtV1DjJ_12PuwE8lGn0FHqadXQ"
      }
     * @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 = self::wxhttp($token_url);

        if ($token_data[0] == 200) {
            return json_decode($token_data[1], TRUE);
        }

        return FALSE;
    }

    /**
     * 获取授权后的微信用户信息
     * array(10) {
      ["openid"]=>
      string(28) "o9Ns61WqAyXnc8fZV4hUHOtPrCYs"
      ["nickname"]=>
      string(9) "张亚超"
      ["sex"]=>
      int(1)
      ["language"]=>
      string(5) "zh_CN"
      ["city"]=>
      string(6) "郑州"
      ["province"]=>
      string(6) "河南"
      ["country"]=>
      string(6) "中国"
      ["headimgurl"]=>
      string(130) "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLUyYMakib59FtzxqgVFpP18mcXKwAZIn4z9n9SluvjdNoic3njLlOgku3g4OGQaxQ4wTwmicoTTFVIQ/132"
      ["privilege"]=>
      array(0) {
      }
      ["unionid"]=>
      string(28) "o1WtV1DjJ_12PuwE8lGn0FHqadXQ"
      }
     * @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 = self::wxhttp($info_url);

            if ($info_data[0] == 200) {
                return json_decode($info_data[1], TRUE);
            }
        }

        return FALSE;
    }

    public static function wxhttp($url, $method = 'POST', $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);
    }

}
上一篇下一篇

猜你喜欢

热点阅读