QQ登录OAuth2.0 php接入种
QQ登录OAuth2.0 php接入类
?
/** ? * ? * qq登录 ? * @author http://www.heui.org ? * ? */ class Oauth_qq { ?? private static $_instance ; ?? private $config = array (); ?
?? private function __construct( $config ) ?? { ???? $this ->Oauth_qq( $config ); ?? } ?
?? public static function getInstance( $config ) ?? { ???? if (!isset(self:: $_instance )) ???? { ?????? $c = __CLASS__ ; ?????? self:: $_instance = new $c ( $config ); ???? } ???? return self:: $_instance ; ?? } ?
?? private function Oauth_qq( $config ) ?? { ???? $this ->config = $config ; ???? $_SESSION [ "appid" ]??? = $this ->config[ 'appid' ]; ???? $_SESSION [ "appkey" ]?? = $this ->config[ 'appkey' ]; ???? $_SESSION [ "callback" ] = $this ->config[ 'callback' ]; ???? $_SESSION [ "scope" ] = "get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo" ; ?? } ?
?? function login() ?? { ???? $_SESSION [ 'state' ] = md5(uniqid(rand(), TRUE)); //CSRF protection ???? $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" ???? . $_SESSION [ "appid" ] . "&redirect_uri=" . urlencode( $_SESSION [ "callback" ]) ???? . "&state=" . $_SESSION [ 'state' ] ???? . "&scope=" . $_SESSION [ "scope" ]; ???? header( "Location:$login_url" ); ?? } ?
?? function callback() ?? { ???? if ( $_REQUEST [ 'state' ] == $_SESSION [ 'state' ]) //csrf ???? { ?????? $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&" ?????? . "client_id=" . $_SESSION [ "appid" ]. "&redirect_uri=" . urlencode( $_SESSION [ "callback" ]) ?????? . "&client_secret=" . $_SESSION [ "appkey" ]. "&code=" . $_REQUEST [ "code" ]; ?
?????? $response = get_url_contents( $token_url ); ?????? if ( strpos ( $response , "callback" ) !== false) ?????? { ???????? $lpos = strpos ( $response , "(" ); ???????? $rpos = strrpos ( $response , ")" ); ???????? $response ? = substr ( $response , $lpos + 1, $rpos - $lpos -1); ???????? $msg = json_decode( $response ); ???????? if (isset( $msg ->error)) ???????? { ?????????? echo "<h3 id="error">error:</h3>" . $msg ->error; ?????????? echo "<h3 id="msg">msg? :</h3>" . $msg ->error_description; ?????????? exit ; ???????? } ?????? } ?
?????? $params = array (); ?????? parse_str ( $response , $params ); ?
?????? $_SESSION [ "access_token" ] = $params [ "access_token" ]; ???? } ???? else ???? { ?????? echo ( "The state does not match. You may be a victim of CSRF." ); ???? } ?? } ?
?? function get_openid() ?? { ???? $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" ???? . $_SESSION [ 'access_token' ]; ?
???? $str ? = get_url_contents( $graph_url ); ???? if ( strpos ( $str , "callback" ) !== false) ???? { ?????? $lpos = strpos ( $str , "(" ); ?????? $rpos = strrpos ( $str , ")" ); ?????? $str ? = substr ( $str , $lpos + 1, $rpos - $lpos -1); ???? } ?
???? $user = json_decode( $str ); ???? if (isset( $user ->error)) ???? { ?????? echo "<h3 id="error">error:</h3>" . $user ->error; ?????? echo "<h3 id="msg">msg? :</h3>" . $user ->error_description; ?????? exit ; ???? } ?
???? //set openid to session ???? return $_SESSION [ "openid" ] = $user ->openid; ?? } ?
?? function get_user_info() ?? { ???? $get_user_info = "https://graph.qq.com/user/get_user_info?" ???? . "access_token=" . $_SESSION [ 'access_token' ] ???? . "&oauth_consumer_key=" . $_SESSION [ "appid" ] ???? . "&openid=" . $_SESSION [ "openid" ] ???? . "&format=json" ; ?
???? $info = get_url_contents( $get_user_info ); ???? $arr = json_decode( $info , true); ?
???? return $arr ; ?? } ?
?? public function __clone() ?? { ???? trigger_error( 'Clone is not allow' ,E_USER_ERROR); ?? } ?
} ?
/* 公用函数 */ if (!function_exists( "do_post" )) { ?? function do_post( $url , $data ) ?? { ???? $ch = curl_init(); ???? curl_setopt( $ch , CURLOPT_RETURNTRANSFER, TRUE); ???? curl_setopt( $ch , CURLOPT_POST, TRUE); ???? curl_setopt( $ch , CURLOPT_POSTFIELDS, $data ); ???? curl_setopt( $ch , CURLOPT_URL, $url ); ???? $ret = curl_exec( $ch ); ?
???? curl_close( $ch ); ???? return $ret ; ?? } } if (!function_exists( "get_url_contents" )) { ?? function get_url_contents( $url ) ?? { ???? if ( ini_get ( "allow_url_fopen" ) == "1" ) ???? return file_get_contents ( $url ); ?
???? $ch = curl_init(); ???? curl_setopt( $ch , CURLOPT_RETURNTRANSFER, TRUE); ???? curl_setopt( $ch , CURLOPT_URL, $url ); ???? $result =? curl_exec( $ch ); ???? curl_close( $ch ); ?
???? return $result ; ?? } } |
使用实例:
$config [ 'appid' ]??? = '' ; $config [ 'appkey' ]?? = '' ; $config [ 'callback' ] = '' ; $o_qq = Oauth_qq::getInstance( $config ); ?
//then $o_qq ->login(); //or $o_qq ->callback(); $o_qq ->get_openid(); $o_qq ->get_user_info();
|
原文:http://www.heui.org/archives/454

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

報錯的原因NameResolutionError(self.host,self,e)frome是由urllib3函式庫中的例外類型,這個錯誤的原因是DNS解析失敗,也就是說,試圖解析的主機名稱或IP位址無法找到。這可能是由於輸入的URL位址不正確,或DNS伺服器暫時無法使用所導致的。如何解決解決此錯誤的方法可能有以下幾種:檢查輸入的URL地址是否正確,確保它是可訪問的確保DNS伺服器可用,您可以嘗試在命令行中使用"ping"命令來測試DNS伺服器是否可用嘗試使用IP位址而不是主機名稱來存取網站如果是在代理

PHP函數介紹—get_headers():取得URL的回應頭資訊概述:在PHP開發中,我們經常需要取得網頁或遠端資源的回應頭資訊。 PHP函數get_headers()能夠方便地取得目標URL的回應頭訊息,並以陣列形式傳回。本文將介紹get_headers()函數的用法,以及提供一些相關的程式碼範例。 get_headers()函數的用法:get_header

區別:1、定義不同,url是是統一資源定位符,而html是超文本標記語言;2、一個html中可以有很多個url,而一個url中只能存在一個html頁面;3、html指的是網頁,而url指的是網站位址。

function是函數的意思,是一段具有特定功能的可重複使用的程式碼區塊,是程式的基本組成單元之一,可以接受輸入參數,執行特定的操作,並傳回結果,其目的是封裝一段可重複使用的程式碼,提高程式碼的可重複使用性和可維護性。

session失效通常是由於 session 的生存時間過期或伺服器關閉導致的。其解決方法:1、延長session的生存時間;2、使用持久化儲存;3、使用cookie;4、非同步更新session;5、使用會話管理中介軟體。

PHPSession跨域問題的解決方法在前後端分離的開發中,跨域請求已成為常態。在處理跨域問題時,我們通常會涉及session的使用和管理。然而,由於瀏覽器的同源策略限制,跨域情況下預設無法共享session。為了解決這個問題,我們需要採用一些技巧和方法來實現session的跨域共享。一、使用cookie跨域共享session最常

1.基於session實作簡訊登入1.1簡訊登入流程圖1.2實作發送簡訊驗證碼前端請求說明:說明請求方式POST請求路徑/user/code請求參數phone(電話號碼)回傳值無後端介面實作:@Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1.校驗手機號碼if

JavaScriptCookie使用JavaScriptcookie是記住和追蹤偏好、購買、佣金和其他資訊的最有效方法。更好的訪客體驗或網站統計所需的資訊。 PHPCookieCookie是儲存在客戶端電腦上的文字檔案並保留它們用於追蹤目的。 PHP透明地支援HTTPcookie。 JavaScriptcookie如何運作?您的伺服器將一些資料傳送到訪客的瀏覽器cookie的形式。瀏覽器可以接受cookie。如果存在,它將作為純文字記錄儲存在訪客的硬碟上。現在,當訪客到達網站上的另一個頁面時
