Home > Backend Development > PHP Tutorial > oauth [PHP] Oauth authorization and local encryption

oauth [PHP] Oauth authorization and local encryption

WBOY
Release: 2016-07-28 08:27:16
Original
907 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 providing username and password. To the third party

Keyword: appKey appSecret token(Token)

2.SSOAuthorization

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

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

Click the link below

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

Specific code sso.php file:

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

The above introduces oauth [PHP] Oauth authorization and local encryption, including oauth content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template