Heim >Backend-Entwicklung >PHP-Tutorial >oauth [PHP] Oauth授权和本地加密

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

WBOY
WBOYOriginal
2016-07-28 08:27:16909Durchsuche

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

 

关键字:appKey appSecret token(令牌)

 

2.SSO授权

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

 

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

点击下面的连接

https://graph.qq.com/oauth2.0/authorize?resp//www.qingguow.cn/sso.php

 

具体代码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();

以上就介绍了oauth [PHP] Oauth授权和本地加密,包括了oauth方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn