Tutorial – PHP – Microsoft Graph – implement Office365 into any website


Let's start

Microsoft Graph SDK for PHP

You’ll need to install microsoft/microsoft-graph via composer into your PHP project. You can do this by:

composer require microsoft/microsoft-graph

or via your composer file composer.json

{“require”:{“microsoft/microsoft-graph”: “^1.5”}}

If you don’t know how to install composer you can read about it here: How to install composer to your php project

The GIT repository of msgraph-sdk-php can be found here: https://github.com/microsoftgraph/msgraph-sdk-php

The overview of GRAPH can be found here: https://docs.microsoft.com/en-us/graph/overview

ONEDRIVE GRAPH: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/index?view=odsp-graph-online

The complete reference of possible usages with explanation for API v.1.0: https://docs.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0


adapt your php curl so you won't get into trouble

cacert.pem can be downloaded directly from our site: cacert.pem

if you don’t set the certificate you will encounter errors like:

RequestException in CurlFactory.php line 187: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

GRAPH has no authentication method included, so you will need to install one with oauth2.

You can do that via  composer require league/oauth2-client

GRAPH will use Guzzle HTTP client which is preinstalled, so you're good to go with this. Unfortunately in my coding I encountered that http_errors are internally handled by middleware and there was no way round so my app was crashing without anything I could do to catch the exceptions. I therefore built an own Guzzle client with http_errors disabled and gave that one over to GRAPH . This way I can look for errors, if there are none, I can continue. See the following example of how to use our custom GUZZLE client.
and here we see how to inject it in GRAPH:
Read about how to get your app tenant id and secret in this article: Tutorial – Azure AD Webapp registration Application Registry Portal (ARP)
Let's assume our project is Larvel based, so we'll go with that assumption. But it will work with any other PHP just as good. In order to make everything work smooth you'll need some few PHP files created. Inside our app we will need a controller for authentication. In Laravel we would do that by
[dt_code]php artisan make:controller AuthController[/dt_code]
Following we see the full code of our controller:
You might say, that I could use env('OAUTH_APP_ID') and put all my variables to my .env file. Yes' - you're right, but if I don't pay attention and forget to use

php artisan config:clear

My app won't work.
It's not really a big deal to have it here, so I put my vars inside of my PHP.
If you want so I could also access these vars from anywhere in my AuthController, as I need to authenticate anyway.
Let's now create our TokenStorage.
Add to your app folder a folder named TokenStore

Please take note that we are working with callbacks in order to get our access tokens, so we need to provide the corresponding function for this callback "localhost\mscallback" or however you want to name this endpoint, also replace localhost with your domain. We will do that later.

This is the content of our TokenCache.php:
You see that we point each route to a specific function inside of our AuthController.

You see that we point each route to a specific function inside of our AuthController.

To have a very rudimentary Login and Logout possibility for connection with GRAPH you can use the following HTML:

If you have questions feel free to ask me or leave me a comment.
7 ratings, 3 votes7 ratings, 3 votes (+7 rating, 3 votes, rated)
You need to be a registered member to rate this.

Leave a Comment

Your email address will not be published. Required fields are marked *

Free Instant SEO Audit

Powered by


Scroll to Top