Hey there, fellow developer! Ready to dive into the world of Microsoft Graph API? We're going to use the microsoft/microsoft-graph
package to build a slick Outlook integration in PHP. Buckle up, because this is going to be a fun ride!
Before we jump in, make sure you've got:
Let's kick things off by installing our star player:
composer require microsoft/microsoft-graph
Easy peasy, right?
Now for the "fun" part - authentication. We'll be using OAuth 2.0, so grab a coffee and let's dive in:
Don't worry if it seems a bit complex at first. Once you've done it a couple of times, it'll be second nature.
Time to get our hands dirty with some code:
require_once __DIR__ . '/vendor/autoload.php'; use Microsoft\Graph\Graph; use Microsoft\Graph\Model; $graph = new Graph(); $graph->setAccessToken('YOUR_ACCESS_TOKEN'); try { $user = $graph->createRequest("GET", "/me") ->setReturnType(Model\User::class) ->execute(); echo "Hello, " . $user->getDisplayName() . "!"; } catch(Exception $e) { echo "Oops, something went wrong: " . $e->getMessage(); }
Now that we're all set up, let's do some cool stuff:
$messages = $graph->createRequest("GET", "/me/messages") ->setReturnType(Model\Message::class) ->execute();
$message = new Model\Message(); $message->setSubject("Hello from Graph API!"); $message->setBody(new Model\ItemBody(["content" => "This is the body"])); $message->setToRecipients([ new Model\Recipient([ "emailAddress" => [ "address" => "[email protected]" ] ]) ]); $graph->createRequest("POST", "/me/sendMail") ->attachBody(["message" => $message]) ->execute();
$event = new Model\Event(); $event->setSubject("Team Meeting"); $event->setStart(new Model\DateTimeTimeZone(["dateTime" => "2023-06-01T09:00:00", "timeZone" => "UTC"])); $event->setEnd(new Model\DateTimeTimeZone(["dateTime" => "2023-06-01T10:00:00", "timeZone" => "UTC"])); $graph->createRequest("POST", "/me/events") ->attachBody($event) ->execute();
Feeling adventurous? Let's explore some advanced features:
$subscription = new Model\Subscription(); $subscription->setChangeType("created,updated"); $subscription->setNotificationUrl("https://your-app.com/webhook"); $subscription->setResource("/me/mailFolders('Inbox')/messages"); $subscription->setExpirationDateTime(new \DateTime("+1 week")); $graph->createRequest("POST", "/subscriptions") ->attachBody($subscription) ->execute();
$batch = $graph->createCollectionRequest("POST", "/\$batch") ->addHeaders(["Content-Type" => "application/json"]) ->attachBody([ "requests" => [ [ "id" => "1", "method" => "GET", "url" => "/me/messages?$select=subject,from&$top=1" ], [ "id" => "2", "method" => "GET", "url" => "/me/calendar/events?$select=subject,organizer&$top=1" ] ] ]); $responses = $batch->execute();
Remember, even the best of us encounter errors. Always wrap your requests in try-catch blocks and handle those exceptions gracefully. And don't forget about rate limiting - be kind to the API!
Last but not least, let's talk testing. PHPUnit is your friend here. Mock those API responses and test away! Your future self (and your team) will thank you.
And there you have it! You're now equipped to build some awesome Outlook integrations with the Microsoft Graph API. Remember, the official documentation is your best friend for diving deeper. Now go forth and code something amazing!