Back

Step by Step Guide to Building a Microsoft Outlook API Integration in PHP

Jul 31, 20246 minute read

Introduction

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!

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment (you're a pro, so I'm sure you've got this covered)
  • Composer (because who doesn't love dependency management?)
  • A Microsoft Azure account (if you don't have one, now's the perfect time to get one)
  • An application registered in Azure AD (trust me, it's easier than it sounds)

Installation

Let's kick things off by installing our star player:

composer require microsoft/microsoft-graph

Easy peasy, right?

Authentication

Now for the "fun" part - authentication. We'll be using OAuth 2.0, so grab a coffee and let's dive in:

  1. Configure your OAuth 2.0 settings in your Azure AD app
  2. Use the authorization code flow to get that precious access token

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.

Basic Setup

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(); }

Common Outlook Operations

Now that we're all set up, let's do some cool stuff:

Retrieving Emails

$messages = $graph->createRequest("GET", "/me/messages") ->setReturnType(Model\Message::class) ->execute();

Sending Emails

$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();

Managing Calendar Events

$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();

Advanced Features

Feeling adventurous? Let's explore some advanced features:

Webhook Subscriptions

$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 Requests

$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();

Error Handling and Best Practices

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!

Testing

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.

Conclusion

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!