首頁 > 後端開發 > php教程 > oauth [PHP] Oauth授權和本地加密

oauth [PHP] Oauth授權和本地加密

WBOY
發布: 2016-07-28 08:27:16
原創
888 人瀏覽過

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();
登入後複製
具體程式碼sso.php檔案:

以上就介紹了oauth [PHP] Oauth授權和本地加密,包括了oauth方面的內容,希望對PHP教程有興趣的朋友有所幫助。
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板