Home > php教程 > php手册 > [PHP] Oauth authorization and local encryption

[PHP] Oauth authorization and local encryption

WBOY
Release: 2016-07-11 08:43:39
Original
1129 people have browsed it

1. Oauth (Open Authorization) is an open standard that allows users to let third-party applications access the user's private resources (such as photos, videos, contact lists) stored on a website without having to Username and password provided to third parties

Keyword: appKey appSecret token(Token)

2.SSOAuthorization

If the local mobile phone is equipped with Weibo client, jump directly to the Weibo client and just click the authorization button to log in

QQ third-party login is implemented using Oauth2.0, test code

Click the link below

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

Specific code sso.php file:

<?<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();
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template