首页 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脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)