PHP code example of ape-dev-de / microsoft-graph-sdk

1. Go to this page and download the library: Download ape-dev-de/microsoft-graph-sdk 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/ */

    

ape-dev-de / microsoft-graph-sdk example snippets




use ApeDevDe\MicrosoftGraphSdk\GraphServiceClient;
use ApeDevDe\MicrosoftGraphSdk\Authentication\BearerTokenAuthenticationProvider;
use Symfony\Component\HttpClient\Psr18Client;
use Nyholm\Psr7\Factory\Psr17Factory;

// Create HTTP client and factories
$httpClient = new Psr18Client();
$psr17Factory = new Psr17Factory();

// Create authentication provider
$authProvider = new BearerTokenAuthenticationProvider('YOUR_ACCESS_TOKEN');

// Create Graph client
$graphClient = GraphServiceClient::create(
    $httpClient,
    $psr17Factory,
    $psr17Factory,
    $authProvider
);

// Use the SDK
$user = $graphClient->me()->get();
echo "Hello, " . $user->getDisplayName();



use ApeDevDe\MicrosoftGraphSdk\GraphServiceClient;
use ApeDevDe\MicrosoftGraphSdk\Authentication\ClientCredentialsAuthenticationProvider;
use Symfony\Component\HttpClient\Psr18Client;
use Nyholm\Psr7\Factory\Psr17Factory;

$httpClient = new Psr18Client();
$psr17Factory = new Psr17Factory();

$authProvider = new ClientCredentialsAuthenticationProvider(
    'YOUR_TENANT_ID',
    'YOUR_CLIENT_ID',
    'YOUR_CLIENT_SECRET',
    $httpClient,
    $psr17Factory,
    $psr17Factory
);

$graphClient = GraphServiceClient::create(
    $httpClient,
    $psr17Factory,
    $psr17Factory,
    $authProvider
);

$me = $graphClient->me()->get();
echo $me->getDisplayName();
echo $me->getMail();

$users = $graphClient->users()->get();

// Collections implement ArrayAccess, Countable, and IteratorAggregate
foreach ($users as $user) {
    echo $user->getDisplayName() . "\n";
}

// Or use traditional getValue()
foreach ($users->getValue() as $user) {
    echo $user->getDisplayName() . "\n";
}

$user = $graphClient->users()->byId('user-id')->get();
echo $user->getDisplayName();

use ApeDevDe\MicrosoftGraphSdk\Models\User;

$newUser = new User();
$newUser->setDisplayName('John Doe');
$newUser->setUserPrincipalName('[email protected]');
$newUser->setMailNickname('johndoe');
$newUser->setAccountEnabled(true);

$createdUser = $graphClient->users()->post($newUser);

$user = $graphClient->users()->byId('user-id')->get();
$user->setJobTitle('Senior Developer');

$updatedUser = $graphClient->users()->byId('user-id')->patch($user);

$graphClient->users()->byId('user-id')->delete();

$messages = $graphClient->me()->messages()->get();

// Direct iteration
foreach ($messages as $message) {
    echo $message->getSubject() . "\n";
}

// Array-like access
echo $messages[0]->getSubject();  // First message
echo count($messages);  // Count messages

// Select specific fields
$user = $graphClient->me()->get([
    '$select' => 'displayName,mail,jobTitle'
]);

// Filter users
$users = $graphClient->users()->get([
    '$filter' => "startsWith(displayName,'John')",
    '$top' => 10
]);

// Order by
$users = $graphClient->users()->get([
    '$orderby' => 'displayName'
]);

$users = $graphClient->users()->get();

// ArrayAccess - Access like an array
$firstUser = $users[0];
$secondUser = $users[1];

// Countable - Use count() directly
$count = count($users);

// IteratorAggregate - Direct iteration
foreach ($users as $user) {
    echo $user->getDisplayName() . "\n";
}

// Check existence
if (isset($users[0])) {
    echo "Has users!";
}

// Traditional method still works
$users->getValue();  // Returns array of User objects

$user = $graphClient->me()->get();

// businessPhones is typed as array with string[] PHPDoc
$phones = $user->getBusinessPhones();  // Returns: string[]

// All array properties work correctly
$licenses = $user->getAssignedLicenses();  // Returns: AssignedLicense[]
$addresses = $user->getProxyAddresses();  // Returns: string[]

[1] > $users = $client->users()->get()
=> UserCollectionResponse { count: 42 }

[2] > $users[0]->getDisplayName()
=> "John Doe"

[3] > count($users)
=> 42

[4] > foreach ($users as $u) { echo $u->getDisplayName() . "\n"; }
John Doe
Jane Smith
...

namespace ApeDevDe\MicrosoftGraphSdk\RequestBuilders;

class CustomUsersRequestBuilder extends UsersRequestBuilder
{
    public function searchByDepartment(string $department): UserCollectionResponse
    {
        return $this->get([
            '$filter' => "department eq '{$department}'"
        ]);
    }
}
bash
php generate.php
bash
php tinker.php
bash
php generate.php