设为首页 加入收藏

TOP

PHP微信扫码登录(一)
2023-07-23 13:28:21 】 浏览:368
Tags:PHP

本文使用php 进行微信pc 扫码登录,扫码获取用户信息

话不多说直接上代码吧!

怎么使用在代码最下面

 

<?php

class WeChatRcLogin
{
    public $state = '';
    public $appid = ''; 
    public $secret = '';
    public $redirect_uri = '';
    public $error = '';
    public $data = [];

    public function __construct($config)
    {
        
        $this->state = $config['state'];
        $this->appid = $config['appid'];
        $this->secret = $config['secret'];
        $this->redirect_uri = $config['redirect_uri'];
    }



    /**
     * 获取登录的url
     * @return string
     * @author: wmq
     * @Time: 2022/10/13 14:40
     */
    public function  getLoginUrl(){
        $arr = [
            'appid' => $this->appid, // appid
            'redirect_uri' => $this->redirect_uri, //回调url
            'response_type' => 'code',
            'scope' => 'snsapi_login',
            'state' => $this->state, //回调验签数据
        ];
        return 'https://open.weixin.qq.com/connect/qrconnect?'.http_build_query($arr).'#wechat_redirect';
    }

    /**
     * 获取access_token
     * @param $param
     * @return bool
     * @author: wmq
     * @Time: 2022/10/13 14:40
     */
    public function getAccessToken($param){
        if(!isset($param['code'],$param['state'])){
            $this->error = '缺少参数';
            return false;
        }
        $arr = [
            'appid' => $this->appid,
            'secret' => $this->secret,
            'code' => $param['code'],
            'grant_type' => 'authorization_code'
        ];
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?'.http_build_query($arr);
        $result = Curl::get($url);
        if(isset($result['errcode'])){
            $this->error = $result['errmsg'];
            return false;
        }else{
            if($this->state && $param['state'] != $this->state){
                $this->error = '验签失败';
                return false;
            }
            $this->data = $result;
            return true;
        }
    }

    /**
     * 获取用户信息
     * @param $param
     * @return bool
     * @author: wmq
     * @Time: 2022/10/13 14:40
     */
    public function getUserInfo($param){
        
        if(!isset($param['access_token'],$param['openid'])){
            $this->error = '缺少参数';
            return false;
        }
        $arr = [
            'access_token' => $param['access_token'],
            'openid' => $param['openid'],
        ];
        $url = 'https://api.weixin.qq.com/sns/userinfo?'.http_build_query($arr);
        $result = Curl::get($url);
        if(isset($result['errcode'])){
            $this->error = $result['errmsg'];
            return false;
        }else{
            $this->data = $result;
            return true;
        }
    }
}

class Curl
{
    /**
     * curl post 请求
     * @param string $url 地址
     * @param array $data 数据
     * @return bool|string
     * @author: wmq
     * @Time: 2022/4/25 9:18
     */
    public static function post($url, $data = [],$header = []){
        return self::curl_request($url,$data,$header);

    }

    /**
     * get请求
     * @param string $url 地址
     * @param int $timeout 时间
     * @return bool|string
     * @author: wmq
     * @Time: 2022/4/25 9:21\
     */
    public static function get($url,$header = []){
        return self::curl_request($url,[],$header);
    }

    public static function curl_request($url,$data = [],$header)
    {
        !$header && $headerArray = ["Content-type:application/json;charset='utf-8'","Accept:application/json"];
        $header && $headerArray = $header;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇TP6框架--CRMEB学习笔记:项目初.. 下一篇微信小程序--云开发

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目