A lightweight PHP library for authenticating and retrieving user data from Microsoft Graph API using OAuth 2.0. This library simplifies the process of obtaining access tokens and fetching user information through Microsoft's authentication system.
- Easy Microsoft OAuth 2.0 authentication
- Token retrieval and management
- Fetch user profile information
- Supports multiple tenants and scopes
- Built with Guzzle HTTP client and Microsoft Graph SDK
- PHP 7.4+
- Composer
- Microsoft Azure Active Directory application credentials
Install the library using Composer:
composer require yosefa/microsoft-graph-api-php- Create a Microsoft Azure AD application in the Azure Portal
- Obtain the following credentials:
- Client ID
- Client Secret
- Tenant ID
- Redirect URI
<?php
use MicrosoftGraphApiPhp\Api;
// Initialize the Microsoft Graph API client
$microsoftGraph = new Api(
$client_id,
$client_secret,
$redirect_uri,
$tenant_id
);
// Generate authorization URL
$authorizationUrl = $microsoftGraph->get_url_auth();
// Redirect user to authorization URL
header("Location: " . $authorizationUrl);
// After user authorization, exchange code for token and retrieve user data
$userData = $microsoftGraph->get_data($authorizationCode);
if ($userData->valid) {
// Access user information
echo "User Name: " . $userData->getDisplayName();
}public function __construct(
$client_id,
$client_secret,
$redirect_uri,
$tenant_id = "common",
$scopes = array("User.Read"),
$state = 42443
)get_url_auth(): Generate Microsoft OAuth authorization URLget_token($code): Exchange authorization code for access tokenget_data($code): Retrieve authenticated user's profile information
Default scope is User.Read. You can specify multiple scopes during initialization:
$scopes = ["User.Read", "Mail.Read"];
$microsoftGraph = new Api($client_id, $client_secret, $redirect_uri, $tenant_id, $scopes);The library returns objects with a valid property:
true: Successful operationfalse: Operation failed
Check the valid property and handle errors accordingly.
Contributions are welcome! Please submit pull requests or open issues on the project repository.