OAuth Client for PHP: Integration with Google, GitHub, and VK

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

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-client


Specific 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:



  1. The user clicks the "Log in via provider" button
  2. Your application redirects the user to the provider's authorization page
  3. The user confirms access
  4. The provider returns an authorization code to your callback URI
  5. Your application exchanges the code for an access token
  6. 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 settings
  • login.php — login page
  • callback.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

Recommendations