GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - microsoftgraph/msgraph-sdk-php: Microsoft Graph Library for PHP.

BranchesTags
Open more actions menu

Latest commit

 

History

2,337 Commits
2,337 Commits

Folders and files

NameName
Last commit message
Last commit date
.azure-pipelines
.azure-pipelines
 
 
.github
.github
 
 
.vscode
.vscode
 
 
docs
docs
 
 
src
src
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.release-please-manifest.json
.release-please-manifest.json
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CODE_OF_CONDUCT.md
CODE_OF_CONDUCT.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
SECURITY.md
SECURITY.md
 
 
THIRD PARTY NOTICES
THIRD PARTY NOTICES
 
 
UPGRADING.md
UPGRADING.md
 
 
composer.json
composer.json
 
 
msgraph-sdk-php.yml
msgraph-sdk-php.yml
 
 
phpdoc.dist.xml
phpdoc.dist.xml
 
 
phpstan.neon
phpstan.neon
 
 
phpunit.xml
phpunit.xml
 
 
release-please-config.json
release-please-config.json
 
 

Repository files navigation

Get started with the Microsoft Graph SDK for PHP

Build Status Latest Stable Version Total Downloads PHP Version Require

Install the SDK

Prerequisites

  • PHP 8.2 or later (see PHP supported versions)

You can install the PHP SDK with Composer by editing your composer.json file:

{
    "require": {
        // x-release-please-start-version
        "microsoft/microsoft-graph": "^3.10.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, call wait() 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

  • Documentation

  • Examples

  • Microsoft Graph website

Update Schedule

The Microsoft Graph PHP SDK is scheduled to be updated during the second and fourth week of each month

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 [email protected] with any additional questions or comments.

About

Microsoft Graph Library for PHP.

Topics

devxengmicrosoftgraphsdk

Resources

Readme
License

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy
Activity
Custom properties

Stars

668 stars

Watchers

37 watching

Forks

149 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.