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

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

CURL

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.
Loading…

17 thoughts on “Tutorial – PHP – Microsoft Graph – implement Office365 into any website”

  1. I just couldn’t go away your site before suggesting that I extremely enjoyed the standard info an individual provide in your guests?
    Is going to be again steadily in order to inspect new posts

  2. Superb site you have here but I was curious if you knew of any message boards that
    cover the same topics discussed in this article? I’d really like to be a part of community where I can get feed-back from other experienced people that share the same interest.

  3. grandbracelets.co.uk

    I am not positive the place you are getting
    your info, however good topic. I needs to spend some time learning more or
    figuring out more. Thanks for wonderful information I was looking
    for this info for my mission.

  4. grandbracelets.co.uk

    I got this website from my friend who shared with me
    about this web site and now this time I am browsing this website
    and reading very informative content at this time.

  5. I appreciate, result in I discovered exactly what I was taking a look
    for. You’ve ended my four day long hunt! God Bless you man. Have a nice day.

    Bye

  6. Hi there very cool blog!! Man .. Excellent .. Wonderful ..

    I will bookmark your web site and take the feeds also?
    I’m happy to find so many helpful info right here within the put up, we want
    work out extra techniques on this regard, thank you for sharing.
    . . . . .

  7. Its such as you read my mind! You appear to understand a lot approximately this, like
    you wrote the book in it or something. I think that you can do with some percent
    to pressure the message home a little bit, however other than that,
    this is wonderful blog. An excellent read. I will certainly be back.

  8. Write more, thats all I have to say. Literally, it seems as though you relied on the video to
    make your point. You obviously know what youre talking about, why waste your intelligence on just posting videos to
    your site when you could be giving us something enlightening to read?

  9. forensic accounting,

    This is a topic which is near to my heart… Many thanks! Exactly where are your contact details though?

  10. I’m impressed, I have to admit. Seldom do I encounter a
    blog that’s both educative and entertaining, and let me tell you, you’ve hit the nail
    on the head. The issue is an issue that too few people are speaking
    intelligently about. I am very happy I came across this in my hunt for something
    concerning this.

  11. Its like you learn my mind! You seem to grasp so much about this, like you wrote the guide in it or
    something. I believe that you just can do with some p.c.
    to pressure the message house a bit, but instead of that,
    that is wonderful blog. A fantastic read. I’ll certainly
    be back.

  12. Great post. I was checking continuously this weblog and I am inspired!
    Very useful information specially the closing part 🙂 I
    take care of such info a lot. I used to be seeking
    this certain information for a long time. Thank
    you and good luck.

  13. best web hosting sites

    You are so awesome! I do not suppose I’ve truly read something like that before.
    So good to find someone with some original thoughts on this issue.
    Seriously.. thanks for starting this up. This web site is
    something that’s needed on the web, someone with some originality!

  14. adreamoftrains web hosting providers

    I want to to thank you for this very good read!!
    I definitely enjoyed every little bit of it. I have got you
    book-marked to look at new things you post… adreamoftrains website hosting

Leave a Comment

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

Free Instant SEO Audit

Powered by

CONTACT US

Scroll to Top