PHP實(shí)現(xiàn)微信網(wǎng)頁授權(quán)開發(fā)教程
微信網(wǎng)頁授權(quán)是服務(wù)號才有的高級功能,開發(fā)者可以通過授權(quán)后獲取用戶的基本信息;在此之前,想要獲取消息信息只能在用戶和公眾號交互時(shí)根據(jù)openid獲取用戶信息;而微信網(wǎng)頁授權(quán)可在不需要消息交互,也不需要關(guān)注的情況下獲取用戶的基本信息。

微信網(wǎng)頁授權(quán)時(shí)通過OAuth2.0完成的,整個(gè)過程分為三步:
- 用戶授權(quán),獲取code;
- 根據(jù)code獲取access_token【可通過refresh_token刷新獲取較長有效期】
- 通過access_token和openid獲取用戶信息
對微信網(wǎng)頁授權(quán)過程做了簡單封裝:
<?php
/**
* 微信授權(quán)相關(guān)接口
*/
class Wechat {
//高級功能-》開發(fā)者模式-》獲取
private $app_id = 'xxx';
private $app_secret = 'xxxxxxx';
/**
* 獲取微信授權(quán)鏈接
*
* @param string $redirect_uri 跳轉(zhuǎn)地址
* @param mixed $state 參數(shù)
*/
public function get_authorize_url($redirect_uri = '', $state = '')
{
$redirect_uri = urlencode($redirect_uri);
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
}
/**
* 獲取授權(quán)token
*
* @param string $code 通過get_authorize_url獲取到的code
*/
public function get_access_token($app_id = '', $app_secret = '', $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;
}
/**
* 獲取授權(quán)后的微信用戶信息
*
* @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, $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_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);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- 微信公眾號-獲取用戶信息(網(wǎng)頁授權(quán)獲?。?shí)現(xiàn)步驟
- C#實(shí)現(xiàn)的微信網(wǎng)頁授權(quán)操作邏輯封裝示例
- php版微信公眾平臺之微信網(wǎng)頁登陸授權(quán)示例
- MVC微信網(wǎng)頁授權(quán)獲取用戶OpenId
- 微信開發(fā) 網(wǎng)頁授權(quán)獲取用戶基本信息
- VueJs單頁應(yīng)用實(shí)現(xiàn)微信網(wǎng)頁授權(quán)及微信分享功能示例
- 微信公眾平臺網(wǎng)頁授權(quán)獲取用戶基本信息中授權(quán)回調(diào)域名設(shè)置的變動(dòng)
- 解決一個(gè)微信號同時(shí)支持多個(gè)環(huán)境網(wǎng)頁授權(quán)問題
相關(guān)文章
在VS?Code?中調(diào)試遠(yuǎn)程服務(wù)器的PHP代碼詳解
這篇文章主要介紹了在VSCode中調(diào)試遠(yuǎn)程服務(wù)器的PHP代碼,這里通過xdebug配合vscode的php?debug插件來實(shí)現(xiàn),本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
ThinkPHP中create()方法自動(dòng)驗(yàn)證實(shí)例
下面小編就為大家?guī)硪黄猅hinkPHP中create()方法自動(dòng)驗(yàn)證實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
ThinkPHP中類的構(gòu)造函數(shù)_construct()與_initialize()的區(qū)別詳解
這篇文章主要介紹了ThinkPHP中類的構(gòu)造函數(shù)_construct()與_initialize()的區(qū)別,文中介紹的非常詳細(xì),相信對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
yii2.0實(shí)現(xiàn)pathinfo的形式訪問的配置方法
這篇文章主要介紹了yii2.0實(shí)現(xiàn)pathinfo的形式訪問的配置方法的相關(guān)資料,需要的朋友可以參考下2016-04-04
Laravel框架FormRequest中重寫錯(cuò)誤處理的方法
這篇文章主要介紹了Laravel框架FormRequest中重寫錯(cuò)誤處理的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02

