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

[PHP] Oauth授权和本地加密

WBOY
Libérer: 2016-07-11 08:43:39
original
1133 Les gens l'ont consulté

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();
Copier après la connexion

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal