OAuth Client for PHP: Complete Integration Guide
In modern web development, OAuth (Open Authorization) has become the de facto standard for organizing login via third-party services. An OAuth client for PHP allows your users to authorize on your site using Google, GitHub, VKontakte, and other provider accounts without needing to create a separate registration.
In this article, we will look at the popular PHP library League OAuth2 Client, which provides a unified interface for working with various OAuth2 providers.
What is OAuth2 and Why Do You Need It?
OAuth2 is an authorization protocol that allows you to delegate access to user resources. Instead of storing passwords from third-party services, you receive a temporary access token. The main advantages are:
- Security — user passwords are not transmitted to your application
- Convenience — users log in with one click
- Trust — lower risk of data leakage
- Additional data — you can obtain email, name, avatar, and other information
Installing the League OAuth2 Client Library
For working with OAuth2 in PHP, it is recommended to use the official package league/oauth2-client. Installation is done via Composer:
composer require league/oauth2-clientSpecific providers require additional packages. For example:
composer require league/oauth2-google # Googlecomposer require league/oauth2-github # GitHubcomposer require digitalcloud/oauth2-vk # VK (unofficial)Basic Architecture of an OAuth2 Client
Any OAuth2 client works according to the following scheme:
- The user clicks the "Log in via provider" button
- Your application redirects the user to the provider's authorization page
- The user confirms access
- The provider returns an authorization code to your callback URI
- Your application exchanges the code for an access token
- Using the token, you obtain user data from the provider's API
Example of Integration with Google
Let's look at a complete working example for authorization via Google. First, create a project in Google Cloud Console and obtain a Client ID and Client Secret.
File structure:
config.php— application settingslogin.php— login pagecallback.php— handler for the response from Google
config.php
<?php// OAuth settings for Googledefine('GOOGLE_CLIENT_ID', 'your_client_id.apps.googleusercontent.com');define('GOOGLE_CLIENT_SECRET', 'your_client_secret');define('GOOGLE_REDIRECT_URI', 'https://your-site.ru/callback.php');
// Include the libraryrequire_once 'vendor/autoload.php';
use League\\OAuth2\\Client\\Provider\\Google;
// Create the provider$provider = new Google([ 'clientId' => GOOGLE_CLIENT_ID, 'clientSecret' => GOOGLE_CLIENT_SECRET, 'redirectUri' => GOOGLE_REDIRECT_URI,]);?>login.php
<?phprequire_once 'config.php';
// Generate the authorization URL$authUrl = $provider->getAuthorizationUrl([ 'scope' => ['email', 'profile'], // request email and profile]);
// Save the state to protect against CSRF attacks$_SESSION['oauth2state'] = $provider->getState();
// Redirect the userheader('Location: ' . $authUrl);exit;?>
<!-- Alternatively: an HTML button --><a href="<?= htmlspecialchars($authUrl) ?>">Log in via Google</a>callback
Also in library
gRPC for Swift (iOS/macOS): Libraries, Setup, and Code Examples
Task Queues for Swift: Complete Guide to GCD and Operation Queues
Object Serialization in Swift (iOS/macOS): Codable, JSON and PropertyList
Redis Client (Jedis) for Java: Complete Guide with Examples
Generating QR Codes in Java: ZXing Library (Detailed Guide)