PHP code example of prasadchinwal / microsoft-graph
1. Go to this page and download the library: Download prasadchinwal/microsoft-graph library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use PrasadChinwal\MicrosoftGraph\Facades\MicrosoftGraph;
// Via Facade
$graph = app('graph');
// Or direct instantiation
$graph = new \PrasadChinwal\MicrosoftGraph\MicrosoftGraph();
use PrasadChinwal\MicrosoftGraph\Facades\MicrosoftGraph;
$users = MicrosoftGraph::users()->get();
foreach ($users as $user) {
echo $user->displayName . ' - ' . $user->mail;
}
// Get events within a date range
$events = MicrosoftGraph::event()
->for('[email protected]')
->where('start/dateTime', 'ge', '2024-01-01')
->where('end/dateTime', 'le', '2024-12-31')
->get();
// Use OR conditions
$events = MicrosoftGraph::event()
->for('[email protected]')
->where('subject', 'eq', 'Team Meeting')
->orWhere('subject', 'eq', 'All Hands')
->get();
namespace App\Mail;
use PrasadChinwal\MicrosoftGraph\Calendar\Event\CalendarEvent;
use PrasadChinwal\MicrosoftGraph\Calendar\Event\Eventable\Envelope;
use PrasadChinwal\MicrosoftGraph\Calendar\Event\Eventable\Attendee;
use PrasadChinwal\MicrosoftGraph\Calendar\Event\Eventable\Enums\CalendarEventImportance;
use Illuminate\Mail\Mailables\Content;
use Carbon\Carbon;
class TeamMeetingEvent extends CalendarEvent
{
public function envelope(): Envelope
{
return new Envelope(
from: '[email protected]',
subject: 'Weekly Team Sync',
attendees: [
new Attendee('[email protected]', 'John Doe',
use App\Mail\TeamMeetingEvent;
$response = MicrosoftGraph::event()->create(new TeamMeetingEvent());
$response = MicrosoftGraph::event()->update(
eventId: 'AAMkAGI2...',
mailable: new TeamMeetingEvent()
);
$response = MicrosoftGraph::attachments()
->forMessage('[email protected]', 'messageId')
->getRaw('attachmentId');
// Save to file
file_put_contents('downloaded_file.pdf', $response->body());
// Or return as HTTP response
return response($response->body())
->header('Content-Type', $response->header('Content-Type'));
use PrasadChinwal\MicrosoftGraph\Builder\Attachment\FileAttachmentBuilder;
// Create attachment from file
$builder = FileAttachmentBuilder::fromFile('/path/to/document.pdf');
$attachment = MicrosoftGraph::attachments()
->forMessage('[email protected]', 'messageId')
->addFile($builder);
echo $attachment->id; // Use this ID for future operations
// If configuration is missing or invalid, a ConfigurationException will be thrown
// Example error messages:
// "Microsoft Graph configuration 'TENANT_ID' is not set. Please ensure MICROSOFT_GRAPH_TENANT_ID is set in your .env file."
// "Microsoft Graph configuration 'timezone' is invalid: Invalid timezone identifier"
$graph = new MicrosoftGraph();
$graph->clearTokenCache();
'timezone' => 'Europe/London',
$user = MicrosoftGraph::users()->find('[email protected]');
// Full IDE autocomplete support
$user->displayName; // string
$user->accountEnabled; // bool
$user->businessPhones; // array
$user->interests; // array|null
use Illuminate\Support\Facades\Http;
public function test_user_retrieval()
{
Http::fake([
'graph.microsoft.com/*' => Http::response([
'value' => [
[
'displayName' => 'Test User',
'mail' => '[email protected]',
// ... other fields
]
]
], 200)
]);
$users = MicrosoftGraph::users()->get();
$this->assertCount(1, $users);
$this->assertEquals('Test User', $users->first()->displayName);
}