microsoft / microsoft-graph
The Microsoft Graph SDK for PHP
Installs: 14 872 301
Dependents: 88
Suggesters: 2
Security: 1
Stars: 586
Watchers: 44
Forks: 144
Open Issues: 69
Requires
- php: ^8.0 || ^7.4
- microsoft/microsoft-graph-core: ^2.1.1
Requires (Dev)
- phpstan/phpstan: ^0.12.90 || ^1.0.0
- phpunit/phpunit: ^8.0 || ^9.0
- dev-main
- v2.21.0
- v2.20.0
- v2.19.0
- v2.18.0
- v2.17.0
- v2.16.0
- v2.15.0
- v2.14.1
- v2.14.0
- v2.13.0
- v2.12.0
- v2.11.0
- 2.10.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- 2.0.0-RC27
- 2.0.0-RC26
- 2.0.0-RC25
- 2.0.0-RC24
- 2.0.0-RC23
- 2.0.0-RC22
- 2.0.0-RC21
- 2.0.0-RC20
- 2.0.0-RC19
- 2.0.0-RC18
- 2.0.0-RC17
- 2.0.0-RC16
- 2.0.0-RC15
- 2.0.0-RC14
- 2.0.0-RC13
- 2.0.0-RC12
- 2.0.0-RC11
- 2.0.0-RC10
- 2.0.0-RC9
- 2.0.0-RC8
- 2.0.0-RC7
- 2.0.0-RC6
- 2.0.0-RC5
- 2.0.0-RC4
- 2.0.0-RC3
- 2.0.0-RC2
- 2.0.0-RC1
- 1.110.0
- 1.109.2
- 1.109.1
- 1.109.0
- 1.108.0
- 1.107.0
- 1.106.0
- 1.105.0
- 1.104.0
- 1.103.0
- 1.102.0
- 1.101.0
- 1.100.0
- 1.99.0
- 1.98.0
- 1.97.0
- 1.96.0
- 1.95.0
- 1.94.0
- 1.93.0
- 1.92.0
- 1.91.0
- 1.90.0
- 1.89.0
- 1.88.0
- 1.87.0
- 1.86.0
- 1.85.0
- 1.84.0
- 1.83.0
- 1.82.0
- 1.81.0
- 1.80.0
- 1.79.0
- 1.78.0
- 1.77.0
- 1.76.0
- 1.75.0
- 1.74.0
- 1.73.0
- 1.72.0
- 1.71.0
- 1.70.0
- 1.69.0
- 1.68.0
- 1.67.0
- 1.66.0
- 1.65.0
- 1.64.0
- 1.63.0
- 1.62.0
- 1.61.0
- 1.60.0
- 1.59.0
- 1.58.0
- 1.57.1
- 1.57.0
- 1.56.0
- 1.55.0
- 1.54.0
- 1.53.0
- 1.52.0
- 1.51.0
- 1.50.0
- 1.49.0
- 1.48.0
- 1.47.0
- 1.46.0
- 1.45.0
- 1.44.0
- 1.43.0
- 1.42.0
- 1.41.0
- 1.40.0
- 1.39.0
- 1.38.0
- 1.37.0
- 1.36.0
- 1.35.1
- 1.35.0
- 1.34.0
- 1.33.0
- 1.32.0
- 1.31.0
- 1.30.1
- 1.30.0
- 1.29.0
- 1.27.0
- 1.26.0
- 1.25.0
- 1.24.0
- 1.23.0
- 1.22.0
- 1.21.0
- 1.20.0
- 1.19.0
- 1.18.0
- 1.17.0
- 1.16.0
- 1.15.0
- 1.14.0
- 1.13.0
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.0
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.1
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.1
- 0.1.0
- dev-release-please--branches--main--components--microsoft/microsoft-graph
- dev-kiota/v1.0/pipelinebuild/174332
- dev-fix/national-clouds
- dev-kiota/v1.0/pipelinebuild/171184
- dev-kiota/v1.0/pipelinebuild/171177
- dev-fix/token-caching-docs
- dev-kiota/v1.0/pipelinebuild/168793
- dev-kiota/v1.0/pipelinebuild/167322
- dev-kiota/v1.0/pipelinebuild/167170
- dev-kiota/v1.0/pipelinebuild/163658
- dev-kiota/v1.0/pipelinebuild/160559
- dev-kiota/v1.0/pipelinebuild/160542
- dev-kiota/v1.0/pipelinebuild/160533
- dev-kiota/v1.0/pipelinebuild/150858
- dev-support/1.x
This package is auto-updated.
Last update: 2024-12-17 11:53:37 UTC
README
Install the SDK
You can install the PHP SDK with Composer by editing your composer.json
file:
{ "require": { // x-release-please-start-version "microsoft/microsoft-graph": "^2.21.0" // x-release-please-end } }
Get started with Microsoft Graph
Register your application
Register your application to use the Microsoft Graph API using Microsoft Azure Active Directory in your tenant's Active Directory to support work or school users for your tenant, or multiple tenants.
Create a Token Request Context
A Token Request Context contains the credentials used to authenticate requests. The SDK supports various contexts that align with OAuth 2.0 flows: client_credentials
, authorization_code
and on_behalf_of
with support for secret-based and certificate-based client authentication.
Under the hood, the Token Request Context is passed to an authentication provider which fetches, caches and refreshes access tokens ensuring all requests are authenticated against the Microsoft Identity platform.
The following sample creates a TokenRequestContext that gets access without a user:
<?php use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext; use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider; $tokenRequestContext = new ClientCredentialContext( 'tenantId', 'clientId', 'clientSecret' );
To gets access on behalf of a user:
<?php use Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext; use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider; $tokenRequestContext = new AuthorizationCodeContext( 'tenantId', 'clientId', 'clientSecret', 'authCode', 'redirectUri' );
Note that your application will need to handle redirecting the user to the Microsoft Identity login page to get the authorization_code
that's passed into the AuthorizationCodeContext
.
See for more on the authorization_code
grant flow.
To keep your user signed in across multiple requests within a session, see section on access token management
Initialize a GraphServiceClient
Using the Token Request Context and optional scopes, a GraphServiceClient
can be initialised:
use Microsoft\Graph\GraphServiceClient; // Defaults to using https://graph.microsoft.com/.default scopes $graphServiceClient = new GraphServiceClient($tokenRequestContext); // With specific scopes $scopes = ['User.Read', 'Mail.ReadWrite']; $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
To initialize the GraphServiceClient
with an already acquired access token or to retrieve the access token that the SDK fetches on your behalf, see section on access token management.
For more on Graph client configuration, see more examples
Call Microsoft Graph using the v1.0 endpoint and models
The following is an example that shows how to fetch a user from Microsoft Graph
<?php use Microsoft\Graph\GraphServiceClient; use Microsoft\Kiota\Abstractions\ApiException; use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext; $tokenRequestContext = new ClientCredentialContext( 'tenantId', 'clientId', 'clientSecret' ); $graphServiceClient = new GraphServiceClient($tokenRequestContext); try { $user = $graphServiceClient->users()->byUserId('[userPrincipalName]')->get()->wait(); echo "Hello, I am {$user->getGivenName()}"; } catch (ApiException $ex) { echo $ex->getError()->getMessage(); }
Note: The SDK is designed to make async requests by default. Operations (
get()
,post()
...) return a Promise. To get the result, callwait()
after the operation. Example:->get()->wait();
Note that to calling me()
requires a signed-in user and therefore delegated permissions (obtained using the authorization_code
flow):
<?php use Microsoft\Graph\GraphServiceClient; use Microsoft\Kiota\Abstractions\ApiException; use Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext; $tokenRequestContext = new AuthorizationCodeContext( 'tenantId', 'clientId', 'clientSecret', 'authCode', 'redirectUri' ); $scopes = ['User.Read']; $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes); try { $user = $graphServiceClient->me()->get()->wait(); echo "Hello, I am {$user->getGivenName()}"; } catch (ApiException $ex) { echo $ex->getError()->getMessage(); }
Documentation and resources
Upgrading
For detailed information on breaking changes, bug fixes and new functionality introduced during major upgrades, check out our Upgrade Guide
Develop
Run Tests
Run
vendor/bin/phpunit
from the base directory.
Issues
View or log issues on the Issues tab in the repo.
Contribute
Please read our Contributing guidelines carefully for advice on how to contribute to this repo.
Copyright and license
Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.