PHP Oauth authorization and local encryption implementation method, phpoauth authorization Encryption

Source: Internet
Author: User
Tags oauth openid

PHP Oauth authorization and local encryption implementation method, phpoauth authorization Encryption

1. Oauth(Open authorization) is an open standard that allows users to allow third-party applications to access private resources (such as photos, videos, and contact lists) stored on a user's website ), instead of providing the user name and password to a third party

Keyword: appKey appSecret token)

2. SSO authorization

If the Weibo client is installed on your local mobile phone, you can directly jump to the Weibo client. You only need to click the authorize button to log in.

Qq third-party login using Oauth2.0 implementation, test code

Click connection 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:

<? Php // qq login class Sso {const APP_ID = "101334262"; const APP_KEY = "xxxxxxxxxxxxx"; // initialize public static function init () {header ("content-type: text/html; charset = UTF-8 ");} // main function public static function main () {// Request control $ action =$ _ GET ['action']; if (! Empty ($ action) {Sso ::$ action (); return ;}$ par = 'Grant _ type = authorization_code '. '& client_id = '. sso: APP_ID. '& client_secret = '. sso: APP_KEY. '& code = '. $ _ REQUEST ['code']. '& redirect_uri = '. urlencode ('HTTP: // www.qingguow.cn/sso.php'); $ rec = Sso: postUrlContents ("https://graph.qq.com/oauth2.0/token", $ par); if (strpos ($ rec, 'Access _ token ')! = False) {parse_str ($ rec, $ accessToken); $ openidJson = Sso: getUrlContents ("https://graph.qq.com/oauth2.0/me? Callback = callback & access_token = {$ accessToken ['Access _ token']} "); $ openidJson = str_replace (" callback ("," ", $ openidJson ); $ openidJson = str_replace (");", "", $ openidJson); $ openidJson = json_decode ($ openidJson, true); header ("location: sso. php? Action = getQQinfo & openid = {$ openidJson ['openid']} & access_token = {$ accessToken ['Access _ token']} ");} // obtain the public static function getQQinfo () {Sso: init (); $ openid =$ _ GET ['openid']; $ access_token = $ _ GET ['Access _ token']; $ userJson = Sso: getUrlContents ("https://graph.qq.com/user/get_user_info? Openid = {$ openid} & access_token = {$ access_token} & oauth_consumer_key = ". sso: APP_ID); $ user = json_decode ($ userJson, true); print_r ($ user) ;}// get request data public static function getUrlContents ($ url) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ ch, CURLOPT_HEADER, false); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_REFERER, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE); $ result = curl_exec ($ ch); curl_close ($ ch); return $ result ;} // post request data public static function postUrlContents ($ url, $ data = null) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url ); curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data);} curl_setopt ($ curl, expires, 1 ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output ;}} Sso: main ();

The above PHP Oauth authorization and local encryption implementation methods are all the content shared by the editor. I hope to give you a reference and support for the help house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.