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

oauth [PHP] Oauth authorization and local encryption

Jul 28, 2016 am 08:27 AM
oauth

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
The best way to implement OAuth2.0 using PHP The best way to implement OAuth2.0 using PHP Jun 08, 2023 am 09:09 AM

OAuth2.0 is a protocol used to authorize third-party applications to access user resources. It is now widely used in the Internet field. With the development of Internet business, more and more applications need to support the OAuth2.0 protocol. This article will introduce the best way to implement the OAuth2.0 protocol using PHP. 1. Basic knowledge of OAuth2.0 Before introducing the implementation of OAuth2.0, we need to understand some basic knowledge of OAuth2.0. Authorization type OAuth2.0 protocol determined

PHP development: Implementing OAuth2 service provider using Laravel Passport PHP development: Implementing OAuth2 service provider using Laravel Passport Jun 15, 2023 pm 04:32 PM

With the popularity of mobile Internet, more and more applications require users to authenticate and authorize themselves. OAuth2 is a popular authentication and authorization framework that provides applications with a standardized mechanism to implement these functions. LaravelPassport is an easy-to-use, secure, and out-of-the-box OAuth2 server implementation that provides PHP developers with powerful tools for building OAuth2 authentication and authorization. This article will introduce the use of LaravelPassport

PHP and OAuth: Implementing Microsoft Login Integration PHP and OAuth: Implementing Microsoft Login Integration Jul 28, 2023 pm 05:15 PM

PHP and OAuth: Implementing Microsoft login integration With the development of the Internet, more and more websites and applications need to support users to log in using third-party accounts to provide a convenient registration and login experience. Microsoft account is one of the widely used accounts around the world, and many users want to use Microsoft account to log in to websites and applications. In order to achieve Microsoft login integration, we can use the OAuth (Open Authorization) protocol to achieve it. OAuth is an open-standard authorization protocol that allows users to authorize third-party applications to act on their behalf

OAuth in PHP: Create a JWT authorization server OAuth in PHP: Create a JWT authorization server Jul 28, 2023 pm 05:27 PM

OAuth in PHP: Creating a JWT authorization server With the rise of mobile applications and the trend of separation of front-end and back-end, OAuth has become an indispensable part of modern web applications. OAuth is an authorization protocol that protects users' resources from unauthorized access by providing standardized processes and mechanisms. In this article, we will learn how to create a JWT (JSONWebTokens) based OAuth authorization server using PHP. JWT is a type of

How to do Google Drive integration using PHP and OAuth How to do Google Drive integration using PHP and OAuth Jul 31, 2023 pm 04:41 PM

How to do GoogleDrive integration using PHP and OAuth GoogleDrive is a popular cloud storage service that allows users to store files in the cloud and share them with other users. Through GoogleDriveAPI, we can use PHP to write code to integrate with GoogleDrive to implement file uploading, downloading, deletion and other operations. To use GoogleDriveAPI we need to authenticate via OAuth and

How to use OAuth2 with php? How to use OAuth2 with php? Jun 01, 2023 am 08:31 AM

OAuth2 is a widely used open standard protocol for granting access to user resources to third-party applications, such as social networks such as Google, Facebook and Twitter, without transmitting usernames and passwords directly to them. In PHP, you can use ready-made OAuth2 libraries to easily implement the OAuth2 flow, or you can build your own library to implement it. In this article, we will focus on using the ready-made OAuth2 library and how to use OAut through it

OAuth2 authentication method and implementation in PHP OAuth2 authentication method and implementation in PHP Aug 07, 2023 pm 10:53 PM

OAuth2 authentication method and implementation in PHP With the development of the Internet, more and more applications need to interact with third-party platforms. In order to protect user privacy and security, many third-party platforms use the OAuth2 protocol to implement user authentication. In this article, we will introduce the OAuth2 authentication method and implementation in PHP, and attach corresponding code examples. OAuth2 is an authorization framework that allows users to authorize third-party applications to access their resources on another service provider without mentioning

Laravel development: How to implement API OAuth2 authentication using Laravel Passport? Laravel development: How to implement API OAuth2 authentication using Laravel Passport? Jun 13, 2023 pm 11:13 PM

As the use of APIs becomes more widespread, protecting the security and scalability of APIs becomes increasingly critical. OAuth2 has become a widely adopted API security protocol that allows applications to access protected resources through authorization. To implement OAuth2 authentication, LaravelPassport provides a simple and flexible way. In this article, we will learn how to implement APIOAuth2 authentication using LaravelPassport. Lar

See all articles