真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

PHP微信掃碼登錄

本文使用php 進(jìn)行微信pc 掃碼登錄,掃碼獲取用戶信息

閬中網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)公司于2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。

話不多說直接上代碼吧!

怎么使用在代碼最下面

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, //回調(diào)url
            'response_type' => 'code',
            'scope' => 'snsapi_login',
            'state' => $this->state, //回調(diào)驗(yàn)簽數(shù)據(jù)
        ];
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 = '缺少參數(shù)';
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 = '驗(yàn)簽失敗';
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 = '缺少參數(shù)';
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 數(shù)據(jù)
     * @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 時(shí)間
     * @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_POSTFIELDS, $data);
        }
$output = curl_exec($curl);
        curl_close($curl);

$res = json_decode($output,true);

return $res;
    }
}












//使用方法
$config = [
'appid' => '',  //appid 微信開放平臺獲取
    'secret' => '', // secret 微信開放平臺獲取
    'redirect_uri' => '', // 回調(diào)地址,微信開放平臺設(shè)置,掃碼成功后微信調(diào)到并攜帶code,state的地址
    'state' => '' // 回調(diào)簽名驗(yàn)簽 可以為空,可以存到緩存中驗(yàn)證
];

$arr = [];
$l = new WeChatRcLogin($config);

//獲取跳轉(zhuǎn)地址
// $l->getLoginUrl();


//https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Authorized_Interface_Calling_UnionID.html

//access_token 建議根據(jù)expires_in 進(jìn)行緩存 獲取access_token 是有次數(shù)限制的,明細(xì)請看官網(wǎng)文檔

//獲取用戶信息
if($l->getAccessToken($_GET)){  //獲取access_token 根據(jù)state, code 微信回調(diào)會傳過來,
    $data = $l->data; //access_token 信息
    if($l->getUserInfo($l->data)){ // 獲取用戶信息
        echo '獲取成功
'; $arr = array_merge($arr,$l->data); //合并access_token 返回的東西 + 用戶信息返回的東西 var_dump($arr); }else{ //獲取用戶信息失敗 echo '獲取用戶信息失敗
'; var_dump($l->error); } }else{ echo '獲取access_token失敗
'; var_dump($l->error); echo '
'; }

本文題目:PHP微信掃碼登錄
網(wǎng)址分享:http://weahome.cn/article/dsoihss.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部