PHP code example of microsoft / microsoft-graph-core

1. Go to this page and download the library: Download microsoft/microsoft-graph-core library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

microsoft / microsoft-graph-core example snippets



$tokenRequestContext = new ClientCredentialContext(
    'tenantId',
    'clientId',
    'clientSecret'
);
// requests using https://graph.microsoft.com/.default scopes by default
$tokenProvider = new GraphPhpLeagueAccessTokenProvider($tokenRequestContext);
$token = $tokenProvider->getAuthorizationTokenAsync(GraphConstants::REST_ENDPOINT)->wait();

use Microsoft\Graph\Core\Core\Http\GraphClientFactory;

$guzzleConfig = [
    // your preferred guzzle config
];

$httpClient = GraphClientFactory::setClientConfig($guzzleConfig)::create();


use Microsoft\Graph\Core\Core\Http\GraphClientFactory;

class UsageExample
{
    public function run()
    {
        $accessToken = 'xxx';

        $config = [
            'headers' => [
                'Authorization' => 'Bearer '.$accessToken
            ]
        ];

        $httpClient = GraphClientFactory::setClientConfig($config)::create();
        $response = $httpClient->get("/v1.0/me");
        $currentUser = json_decode($response->getBody());

        echo "Hello, I am {$currentUser['givenName']}";
    }
}