PHP code example of yosefa / microsoft-graph-api-php

1. Go to this page and download the library: Download yosefa/microsoft-graph-api-php 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/ */

    

yosefa / microsoft-graph-api-php example snippets



use MicrosoftGraphApiPhp\Api;

// Initialize the Microsoft Graph API client
$microsoftGraph = new Api(
    $client_id,
    $client_secret,
    $redirect_uri,
    $tenant_id
);

// Generate authorization URL
$authorizationUrl = $microsoftGraph->get_url_auth();

// Redirect user to authorization URL
header("Location: " . $authorizationUrl);

// After user authorization, exchange code for token and retrieve user data
$userData = $microsoftGraph->get_data($authorizationCode);

if ($userData->valid) {
    // Access user information
    echo "User Name: " . $userData->getDisplayName();
}

public function __construct(
    $client_id, 
    $client_secret, 
    $redirect_uri, 
    $tenant_id = "common", 
    $scopes = array("User.Read"), 
    $state = 42443
)

$scopes = ["User.Read", "Mail.Read"];
$microsoftGraph = new Api($client_id, $client_secret, $redirect_uri, $tenant_id, $scopes);