首頁 php教程 php手册 [PHP] Oauth授权和本地加密

[PHP] Oauth授权和本地加密

Jul 11, 2016 am 08:43 AM

1.Oauth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方

 

关键字:appKey appSecret token(令牌)

 

2.SSO授权

如果本地手机装有微博客户端,则直接跳转到微博客户端,只需点击授权按钮,就可以登陆了

 

qq第三方登陆使用Oauth2.0实现,测试代码

点击下面的连接

https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101334262&redirect_uri=http://www.qingguow.cn/sso.php

 

具体代码sso.php文件:

<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> qq登陆类</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Sso{
    </span><span style="color: #0000ff;">const</span> APP_ID="101334262"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">const</span> APP_KEY="xxxxxxxxxxxxxxx"<span style="color: #000000;">;
    </span><span style="color: #008000;">//</span><span style="color: #008000;">初始化</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> init(){
        </span><span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">);
    }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">主函数</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> main(){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">请求控制</span>
        <span style="color: #800080;">$action</span>=<span style="color: #800080;">$_GET</span>['action'<span style="color: #000000;">];
        </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$action</span><span style="color: #000000;">)){
            Sso</span>::<span style="color: #800080;">$action</span><span style="color: #000000;">();
            </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
        }
       
        </span><span style="color: #800080;">$par</span> = 'grant_type=authorization_code'
        . '&client_id='.Sso::<span style="color: #000000;">APP_ID
        </span>. '&client_secret='.Sso::<span style="color: #000000;">APP_KEY
        </span>. '&code='.<span style="color: #800080;">$_REQUEST</span>['code'<span style="color: #000000;">]
        </span>. '&redirect_uri='.<span style="color: #008080;">urlencode</span>('http://www.qingguow.cn/sso.php'<span style="color: #000000;">);
        </span><span style="color: #800080;">$rec</span>=Sso::postUrlContents("https://graph.qq.com/oauth2.0/token",<span style="color: #800080;">$par</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$rec</span>, 'access_token') !== <span style="color: #0000ff;">false</span><span style="color: #000000;">) {
            </span><span style="color: #008080;">parse_str</span>(<span style="color: #800080;">$rec</span>, <span style="color: #800080;">$accessToken</span><span style="color: #000000;">);
            </span><span style="color: #800080;">$openidJson</span>=Sso::getUrlContents("https://graph.qq.com/oauth2.0/me?callback=callback&access_token={<span style="color: #800080;">$accessToken</span>['access_token']}"<span style="color: #000000;">);
            </span><span style="color: #800080;">$openidJson</span>=<span style="color: #008080;">str_replace</span>("callback( ", "", <span style="color: #800080;">$openidJson</span><span style="color: #000000;">);
            </span><span style="color: #800080;">$openidJson</span>=<span style="color: #008080;">str_replace</span>(");", "", <span style="color: #800080;">$openidJson</span><span style="color: #000000;">);
            </span><span style="color: #800080;">$openidJson</span>=json_decode(<span style="color: #800080;">$openidJson</span>,<span style="color: #0000ff;">true</span><span style="color: #000000;">);
            </span><span style="color: #008080;">header</span>("location:sso.php?action=getQQinfo&openid={<span style="color: #800080;">$openidJson</span>['openid']}&access_token={<span style="color: #800080;">$accessToken</span>['access_token']}"<span style="color: #000000;">);
        }
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">获取用户信息</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getQQinfo(){
        Sso</span>::<span style="color: #000000;">init();
        </span><span style="color: #800080;">$openid</span>=<span style="color: #800080;">$_GET</span>['openid'<span style="color: #000000;">];
        </span><span style="color: #800080;">$access_token</span>=<span style="color: #800080;">$_GET</span>['access_token'<span style="color: #000000;">];
        </span><span style="color: #800080;">$userJson</span>=Sso::getUrlContents("https://graph.qq.com/user/get_user_info?openid={<span style="color: #800080;">$openid</span>}&access_token={<span style="color: #800080;">$access_token</span>}&oauth_consumer_key=".Sso::<span style="color: #000000;">APP_ID);
        </span><span style="color: #800080;">$user</span>=json_decode(<span style="color: #800080;">$userJson</span>,<span style="color: #0000ff;">true</span><span style="color: #000000;">);
        </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">);
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">get方式请求数据</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getUrlContents(<span style="color: #800080;">$url</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$ch</span> =<span style="color: #000000;"> curl_init();
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: #0000ff;">FALSE</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_HEADER, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_FOLLOWLOCATION, <span style="color: #0000ff;">true</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_URL, <span style="color: #800080;">$url</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_REFERER, <span style="color: #800080;">$url</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #0000ff;">TRUE</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$result</span> = curl_exec(<span style="color: #800080;">$ch</span><span style="color: #000000;">);
        curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;">post请求数据</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> postUrlContents(<span style="color: #800080;">$url</span>,<span style="color: #800080;">$data</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$curl</span> =<span style="color: #000000;"> curl_init();
        curl_setopt(</span><span style="color: #800080;">$curl</span>, CURLOPT_URL, <span style="color: #800080;">$url</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$curl</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: #0000ff;">FALSE</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$curl</span>, CURLOPT_SSL_VERIFYHOST, <span style="color: #0000ff;">FALSE</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">)){
        curl_setopt(</span><span style="color: #800080;">$curl</span>, CURLOPT_POST, 1<span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$curl</span>, CURLOPT_POSTFIELDS, <span style="color: #800080;">$data</span><span style="color: #000000;">);
        }
        curl_setopt(</span><span style="color: #800080;">$curl</span>, CURLOPT_RETURNTRANSFER, 1<span style="color: #000000;">);
        </span><span style="color: #800080;">$output</span> = curl_exec(<span style="color: #800080;">$curl</span><span style="color: #000000;">);
        curl_close(</span><span style="color: #800080;">$curl</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$output</span><span style="color: #000000;">;
    }

}
Sso</span>::main();
登入後複製

 

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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