>> Howdy. I'm Richard diZerega,
a Principal Software Engineer in
Microsoft's Commercial Software Engineering Division.
Today, I'll share a brief overview of
the Microsoft Graph SDK for PHP and
how you can use it to build powerful applications
connected to Microsoft and Office 365.
The PHP SDK is a perfect example
of Microsoft's commitment to the open source community,
and providing SDKs on
platforms that Developers want to use.
Before I jump into a demo,
let me cover some of the highlights of SDK.
First, the Microsoft Graph SDK for
PHP is fully open sourced on GitHub.
So, you can clone that repo or you can simply
incorporate it into your PHP project using Composer.
The SDK is decoupled from authentication,
meaning you can use any authentication approach
you want against Azure Active Directory.
Simply provide the SDK with a valid access
token using the set access token method.
Although Microsoft does not
provide authentication libraries for PHP,
there are many in the open source community,
including one made specifically for OAuth2 with Azure ID.
The PHP SDK is request-based,
and it offers strongly type classes.
This simplifies REST calls,
and all the JSON that goes in and out of the Graph.
You start using it by creating
a request using the desired HTTP action.
So, these are going to be things like a get,
a post, a put,
a patch, then you'll specify
the endpoint that you want to perform that action on.
You could even designate your return type
using the set return type method.
The Graph SDK supports
both the V1 and beta endpoints in the Graph.
Just set the API version when you
create the Graph object that you'll be working with.
In addition to that,
the Graph SDK for PHP supports
both the V1 and beta endpoints of the Graph.
You just simply need to set the API version
when creating your Graph object.
The SDK also supports intelligent paging for
Graph endpoints that might return lots of results.
So rather than dealing with the O data next page details,
the SDK handles all that for you.
You simply create a pointer,
and say, "Hey, am I at the end?
Or, let's get the next page."
Let's take a look at the SDK in more detail using a demo.
I have a starter project here that basically
already has all of the authentication details set up.
So, I have an AuthHelper PHP class
that I use, that I can say,
"Go get an access token,"
and that's the token that I'm going to use
to work with the Microsoft Graph SDK for PHP.
So, at this point, I need to
incorporate that SDK into my project,
and I'm going to do that using Composer.
So, I'm going out to a browser here,
and I'm going to say "composer,"
and say I want to require
"microsoft/microsoft-graph," and that's going
to go pull the Microsoft Graph SDK
for PHP into my project,
and I'll easily be able to work with
that in this project.
So, now that that's finished,
you can see that I have this vendor's folder
here that has that Microsoft Graph SDK,
and I can start using it.
For my page here, all I really need
to use because I'm using Composer
is just the require "Autoload" that composer does for me.
So, I'll go ahead and uncomment that
so that it's included in my project.
And now, we can go and start using this SDK.
And the first thing I want to do in my page
is I'm going to reference a few of
the classes that are offered through the Graph SDK.
So, I'm going to say "use
Microsoft" and I'm going to do "Microsoft Graph\Graph."
And this is the actual Graph client that
we're going to perform operations on.
And then finally, I'm also going
to say "Microsoft\Graph\Model" which
provides all those strongly typed classes that I can use
to work more intelligently
with the data I get from the Graph.
So, here's the point why I actually have an access token.
You can see I have some messages
here and what we're going to display
is messages that I have in my inbox.
Now that we're referencing
both the Graph client and our models,
let's set up an actual Graph client object
so that we can call into the Graph.
So, what I'll do is just define a Graph object here,
that's going to be a new Graph.
And what we need to do is
inject our access token into this.
So, what we're going to do is call Graph,
and we will call the "setAccessToken" method.
Now, the token that we get back from our AuthHelper
actually contains a lot more
details than an access token.
It has a refresh token, an ID token.
And so, what we're going to do is actually
call or send into this
the token and say we want the access token part of it.
Now that we've set our access token,
we should be able to successfully call the Graph.
So, let's actually set this up.
And what we're going to do is query
mail messages out of our account.
And so to do that, that is
potentially could be a lot of mail messages.
So, we're going to use some of
that intelligent paging that I mentioned earlier.
So, we'll set up a pointer here,
and we'll set that equal to Graph.
And what we're going to call on this,
remember, I'm going to create a request.
In this case, it is a request for a collection.
So, we'll say, "createCollectionRequest" and in that,
I need to specify two things.
First, the verb.
We're going to do a "get" but I could just as easily
do a "post" or "delete" or whatever I need to.
And then I need to specify
the endpoint that we're going to perform that action on.
In this case, it's going to be "/me/messages."
That's our endpoint.
We now need to convert what comes back in
that REST request as a strongly typed class.
And the way we do that is on this,
we can call the "setReturnType."
And in this case, the return type we're
expecting is a "Model\Message."
That's the type of class.
So, we'll go ahead and specify that.
And then finally, what we can
do is specify any sort of additional things.
In this case, I could set my page size.
So, we'll do a "setPageSize" and we'll just set it to 10.
We'll get 10 at a time.
Now, that's actually not performing the query,
what is going to perform
the query is when I actually use my pointer.
So, I already have an empty array here of messages.
So, instead of I wanted to just set that
to maybe the first 10 messages,
I could say "ptr.getPage."
So, it's basically fetching a page.
In this case, it's safe to do this the first time around
because I know I'm going to get
some result back, but at some point,
if I'm actually iterating through this,
I might want to check to see if I've
completed all of the queries for this endpoint.
So, I think we have everything we need
here to test this out.
I'm using the PHP debugging extensions
here in Visual Studio code.
So, I can actually test this here in a browser.
We'll go ahead and just start debugging.
And I'll open a browser window
here and will go to localhost
888, build, and index.PHP.
So you can see,
it's having me sign in,
and then it's going to return back with my token,
and we can step over this,
and in this case, it did not find.
Oh, I referenced that wrong to just getting confused with
different versions of different coding platforms.
But we'll fix that here really quick.
And now, if I go back and refresh
our page, we should see,
if I step over this,
is we have messages.
You can see there's a bunch of messages here.
I could go look into the details of these.
But these are strongly typed.
So, I could go bind them to my UI pretty easily.
Here, I'm already iterating through the messages,
so I could do things like let's just do maybe a message,
and let's look at the maybe
"getSender," and then on the sender,
maybe what we want to look at is
"getEmailAddress," and then finally,
we can do a "getName."
So, that'll give me the name that's passed in here.
And I can do this for all of
the properties that I'm interested in.
I'll do more simple ones here,
so we could do something like "getSubject" and so forth.
So, I'll stop at this point and
we'll see if that binds to our UI.
So, we'll go out and refresh this one more time.
I'll just let it play over our break point,
and you can see, we have
our actual properties coming back.
So, super nice here being able to use
that SDK to not have to deal
with all the details of REST and headers.
It handles all of that for us
including a lot of the paging.
You can get started developing with
the Microsoft Graph SDK for
PHP right away using these links.
All of the Microsoft Graph SDKs
are offered open source on GitHub,
so you can submit feedback,
look at issues, and even contribute pull requests.
You'll also find a number of
samples that are out there for
all of the different platforms we support.
Finally, we're active where Developers are active.
So, check us out on StackOverflow and post
your questions using the tag, microsoftgraph.