yosefa / microsoft-graph-api-php
Currently a simple utility for login using microsoft graph api php.
1.0.3
2022-08-04 03:53 UTC
Requires
- php: >=7.2
- microsoft/microsoft-graph: ^1.0
README
Description
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.
Features
- 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
Prerequisites
- PHP 7.4+
- Composer
- Microsoft Azure Active Directory application credentials
Installation
Install the library using Composer:
composer require yosefa/microsoft-graph-api-php
Configuration
- Create a Microsoft Azure AD application in the Azure Portal
- Obtain the following credentials:
- Client ID
- Client Secret
- Tenant ID
- Redirect URI
Usage Example
<?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(); }
Method Reference
Constructor
public function __construct( $client_id, $client_secret, $redirect_uri, $tenant_id = "common", $scopes = array("User.Read"), $state = 42443 )
Methods
get_url_auth()
: Generate Microsoft OAuth authorization URLget_token($code)
: Exchange authorization code for access tokenget_data($code)
: Retrieve authenticated user's profile information
Scopes
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);
Error Handling
The library returns objects with a valid
property:
true
: Successful operationfalse
: Operation failed
Check the valid
property and handle errors accordingly.
Dependencies
Contributing
Contributions are welcome! Please submit pull requests or open issues on the project repository.