grim-reapper / msgraph-php
A comprehensive PHP package for Microsoft Graph API integration with OAuth 2.0 authentication, OneDrive file management, and document conversion capabilities
1.0.0
2025-09-28 13:07 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.5
- league/oauth2-client: ^2.6
- microsoft/microsoft-graph: ^1.109
- psr/log: ^3.0
- psr/simple-cache: ^3.0
- symfony/cache: ^6.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.15
- mockery/mockery: ^1.5
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7
- symfony/var-dumper: ^6.3
README
A comprehensive and robust PHP package for integrating with Microsoft Graph API, providing seamless access to Microsoft 365 services including OneDrive, Outlook, Calendar, Teams, and more.
Features
- ๐ OAuth 2.0 Authentication - Secure authentication with Azure Active Directory
- ๐ OneDrive Integration - Complete file management capabilities
- ๐ Document Conversion - Convert documents (DOCX to PDF, etc.)
- ๐ง Email Operations - Send, read, and manage emails
- ๐ Calendar Management - Create and manage events
- ๐ฅ User Management - User profiles and presence
- ๐ Enterprise Security - Production-ready security practices
- ๐งช Comprehensive Testing - Unit and integration tests
- ๐ Rich Documentation - Complete API documentation and examples
Requirements
- PHP 8.1 or higher
- Composer for dependency management
- Azure AD application for authentication
Installation
composer require grim-reapper/msgraph-php
Quick Start
1. Authentication Setup
First, create an Azure AD application and obtain your credentials:
use GrimReapper\MsGraph\Authentication\AuthConfig; use GrimReapper\MsGraph\Core\GraphClient; $config = new AuthConfig([ 'clientId' => 'your-client-id', 'clientSecret' => 'your-client-secret', 'tenantId' => 'your-tenant-id', 'redirectUri' => 'https://yourapp.com/callback' ]); $graphClient = new GraphClient($config);
2. OneDrive Operations
use GrimReapper\MsGraph\Services\OneDriveService; $oneDrive = new OneDriveService($graphClient); // List files $files = $oneDrive->listFiles('/'); // Upload a file $oneDrive->uploadFile('/path/to/local/file.pdf', 'Documents/file.pdf'); // Download a file $content = $oneDrive->downloadFile('/Documents/file.pdf');
3. Document Conversion
use GrimReapper\MsGraph\Services\DocumentService; $documentService = new DocumentService($graphClient); // Convert DOCX to PDF $convertedContent = $documentService->convertDocument( '/Documents/document.docx', 'pdf' );
4. Email Operations
use GrimReapper\MsGraph\Services\MailService; $mailService = new MailService($graphClient); // Send email $mailService->sendEmail([ 'to' => 'recipient@example.com', 'subject' => 'Test Email', 'body' => 'This is a test email from Microsoft Graph PHP' ]); // Read emails $messages = $mailService->getMessages();
Documentation
- API Documentation - Complete API reference
- Authentication Guide - Setup OAuth 2.0
- Examples - Code examples and use cases
- Contributing - Development guidelines
Configuration
Authentication Options
$config = new AuthConfig([ 'clientId' => 'your-client-id', 'clientSecret' => 'your-client-secret', 'tenantId' => 'your-tenant-id', 'redirectUri' => 'https://yourapp.com/callback', 'scopes' => ['https://graph.microsoft.com/.default'], 'cache' => new FileCache(), // Optional: Custom cache implementation 'logger' => new Logger(), // Optional: Custom logger ]);
Advanced Configuration
// Custom HTTP client $httpClient = new GuzzleHttp\Client([ 'timeout' => 30, 'headers' => ['User-Agent' => 'MyApp/1.0'] ]); $graphClient = new GraphClient($config, $httpClient);
Services Overview
Service | Description | Key Methods |
---|---|---|
OneDriveService |
File management | uploadFile() , downloadFile() , listFiles() , deleteFile() |
DocumentService |
Document conversion | convertDocument() , getSupportedFormats() |
MailService |
Email operations | sendEmail() , getMessages() , getMessage() |
CalendarService |
Calendar management | createEvent() , getEvents() , updateEvent() |
UserService |
User management | getUser() , getUsers() , getUserPhoto() |
Error Handling
use GrimReapper\MsGraph\Exceptions\GraphException; use GrimReapper\MsGraph\Exceptions\AuthenticationException; try { $files = $oneDrive->listFiles('/'); } catch (AuthenticationException $e) { // Handle authentication errors echo "Authentication failed: " . $e->getMessage(); } catch (GraphException $e) { // Handle API errors echo "Graph API error: " . $e->getMessage(); } catch (Exception $e) { // Handle other errors echo "General error: " . $e->getMessage(); }
Testing
# Run all tests composer test # Run with coverage composer test:coverage # Run only unit tests composer test -- --testsuite "Unit Tests" # Run only integration tests composer test -- --testsuite "Integration Tests"
Security
This package implements security best practices:
- Secure token storage and refresh
- Input validation and sanitization
- Protection against common vulnerabilities
- HTTPS enforcement
- Rate limiting support
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Run tests (
composer test
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- ๐ Documentation
- ๐ Issues
- ๐ฌ Discussions
- ๐ง Email Support
Changelog
See CHANGELOG.md for a list of changes and version history.
Made with โค๏ธ by Grim Reapper Corp