1.Oauth(開放授權)是一個開放標準,允許用戶讓第三方應用訪問該用戶在某一網站上存儲的私密資源(如照片,視頻,聯繫人列表),而無需將使用者名稱和密碼提供給第三方
關鍵字:appKey appSecret token(令牌))
2.SSO授權
如果本地手機裝有微博客戶端,則直接跳到微客戶端,只需點選授權按鈕,就可以登陸了 qq第三方登陸使用Oauth2.0實現,測試代碼點擊下面的連接https:>點擊下面的連接
https:: //graph.qq.com/oauth2.0/authorize?resp//www.qingguow.cn/sso.php
<?<span>php </span><span>//</span><span> qq登陆类</span><span>class</span><span> Sso{ </span><span>const</span> APP_ID="101334262"<span>; </span><span>const</span> APP_KEY="xxxxxxxxxxxxxxx"<span>; </span><span>//</span><span>初始化</span><span>public</span><span>static</span><span>function</span><span> init(){ </span><span>header</span>("content-type:text/html;charset=utf-8"<span>); } </span><span>//</span><span>主函数</span><span>public</span><span>static</span><span>function</span><span> main(){ </span><span>//</span><span>请求控制</span><span>$action</span>=<span>$_GET</span>['action'<span>]; </span><span>if</span>(!<span>empty</span>(<span>$action</span><span>)){ Sso</span>::<span>$action</span><span>(); </span><span>return</span><span>; } </span><span>$par</span> = 'grant_type=authorization_code' . '&client_id='.Sso::<span>APP_ID </span>. '&client_secret='.Sso::<span>APP_KEY </span>. '&code='.<span>$_REQUEST</span>['code'<span>] </span>. '&redirect_uri='.<span>urlencode</span>('http://www.qingguow.cn/sso.php'<span>); </span><span>$rec</span>=Sso::postUrlContents("https://graph.qq.com/oauth2.0/token",<span>$par</span><span>); </span><span>if</span>(<span>strpos</span>(<span>$rec</span>, 'access_token') !== <span>false</span><span>) { </span><span>parse_str</span>(<span>$rec</span>, <span>$accessToken</span><span>); </span><span>$openidJson</span>=Sso::getUrlContents("https://graph.qq.com/oauth2.0/me?callback=callback&access_token={<span>$accessToken</span>['access_token']}"<span>); </span><span>$openidJson</span>=<span>str_replace</span>("callback( ", "", <span>$openidJson</span><span>); </span><span>$openidJson</span>=<span>str_replace</span>(");", "", <span>$openidJson</span><span>); </span><span>$openidJson</span>=json_decode(<span>$openidJson</span>,<span>true</span><span>); </span><span>header</span>("location:sso.php?action=getQQinfo&openid={<span>$openidJson</span>['openid']}&access_token={<span>$accessToken</span>['access_token']}"<span>); } } </span><span>//</span><span>获取用户信息</span><span>public</span><span>static</span><span>function</span><span> getQQinfo(){ Sso</span>::<span>init(); </span><span>$openid</span>=<span>$_GET</span>['openid'<span>]; </span><span>$access_token</span>=<span>$_GET</span>['access_token'<span>]; </span><span>$userJson</span>=Sso::getUrlContents("https://graph.qq.com/user/get_user_info?openid={<span>$openid</span>}&access_token={<span>$access_token</span>}&oauth_c>APP_ID); <span>$user</span>=json_decode(<span>$userJson</span>,<span>true</span><span>); </span><span>print_r</span>(<span>$user</span><span>); } </span><span>//</span><span>get方式请求数据</span><span>public</span><span>static</span><span>function</span> getUrlContents(<span>$url</span><span>){ </span><span>$ch</span> =<span> curl_init(); curl_setopt(</span><span>$ch</span>, CURLOPT_SSL_VERIFYPEER, <span>FALSE</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_HEADER, <span>false</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_FOLLOWLOCATION, <span>true</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_URL, <span>$url</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_REFERER, <span>$url</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>TRUE</span><span>); </span><span>$result</span> = curl_exec(<span>$ch</span><span>); curl_close(</span><span>$ch</span><span>); </span><span>return</span><span>$result</span><span>; } </span><span>//</span><span>post请求数据</span><span>public</span><span>static</span><span>function</span> postUrlContents(<span>$url</span>,<span>$data</span> = <span>null</span><span>){ </span><span>$curl</span> =<span> curl_init(); curl_setopt(</span><span>$curl</span>, CURLOPT_URL, <span>$url</span><span>); curl_setopt(</span><span>$curl</span>, CURLOPT_SSL_VERIFYPEER, <span>FALSE</span><span>); curl_setopt(</span><span>$curl</span>, CURLOPT_SSL_VERIFYHOST, <span>FALSE</span><span>); </span><span>if</span> (!<span>empty</span>(<span>$data</span><span>)){ curl_setopt(</span><span>$curl</span>, CURLOPT_POST, 1<span>); curl_setopt(</span><span>$curl</span>, CURLOPT_POSTFIELDS, <span>$data</span><span>); } curl_setopt(</span><span>$curl</span>, CURLOPT_RETURNTRANSFER, 1<span>); </span><span>$output</span> = curl_exec(<span>$curl</span><span>); curl_close(</span><span>$curl</span><span>); </span><span>return</span><span>$output</span><span>; } } Sso</span>::main();