PHP code example of smileandlearn / microsoft-api-php

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

    

smileandlearn / microsoft-api-php example snippets


    // INITIALIZATION
$microsoft = new mAuth($tenant, $client_id,  $client_secret, $callback, $scopes);
    header("location: ". $microsoft->getAuthUrl()); //Redirecting to get access token

// ON CALLBACK
session_start(); 
th;
use Smileandlearn\Microsoft\Handlers\mSession;

$microsoft = new mAuth(mSession::get("tenant_id"),mSession::get("client_id"),  mSession::get("client_secret"), mSession::get("redirect_uri"), mSession::get("scopes"));
$tokens = $microsoft->getToken($_REQUEST['code'], mSession::get("state"));

// Setting access token to the wrapper
$microsoft->setAccessToken($tokens->access_token);
 

use Smileandlearn\Microsoft\Models\User;

$user = (new mUser); // User get pulled only if access token was generated for scope User.Read
echo $user->data->getGivenName(); // Adnan
echo $user->data->getOnPremisesImmutableId(); // [email protected]

 

// ON CALLBACK
session_start(); 
th;
use Smileandlearn\Microsoft\Handlers\mSession;

$microsoft = new mAuth(mSession::get("tenant_id"),mSession::get("client_id"),  mSession::get("client_secret"), mSession::get("redirect_uri"), mSession::get("scopes"));
$tokens = $microsoft->getToken($_REQUEST['code'], mSession::get("state"));

$refreshToken = $tokens->refresh_token;
 


$refreshToken = "M.R3_BAY.CbCa*dfsafayrRe9NFNcFEWJBZF9*sXaIYH1HHEFb6i2uUFCGT0KvyXzXulrjPqC3qRgw*NAuajBICU6PmdvfHOyeWGdmE8tUZ4f6XSluF3aKHBGbs*FGSvY7nkUgHhJ*F*4Pfg6SLuNNHY8mh6U8pMNuY1EwnKgAI9s1X4Tt0VXm*mIeLoiw8MTifTukr1aK!7rQOA18ow84bOSpPyu7lZbwATC2pygflRZEOPiHi2!MGrw6CuCxLPgGVu88rsWZJJw3rLjSTofJF78Sgb8ZjkIJAwcfZukotN0lF0GaTThWvM35QEricRyVBYxIC*8iXywFmqKkeClJFeVYx!US35inDel3oXg9**jtd8FAN7x!050JGWN7iJgJA!eMg4h1L6PjcmCZfuVnv0s5eGJ3jauimRBPKJLT6rgzVvkAtI5mJitumZzKnzQNRCxn03w$$";

$microsoft = new mAuth(mSession::get("tenant_id"),mSession::get("client_id"),  mSession::get("client_secret"), mSession::get("redirect_uri"), mSession::get("scopes"));
 
$microsoft->setRefreshToken($refreshToken);
$accessToken = $auth->setAccessToken();

$user = (new mUser); // User get pulled only if refresh token was generated for scope User.Read
echo $user->data->getGivenName(); // Adnan
echo $user->data->getOnPremisesImmutableId(); // [email protected]

 

session_start(); // Important
mAuth;

$tenant = "common"; 
$client_id = "6b152c50-4225-48f8-b824-..........";
$client_secret = "jXcajMv~SAFDDF~GBbvNPM7_Q0v5j02_p.";
$scopes = [
            "User.Read",
            'Files.Read.All',
            '.......'
        ];
$callback_url = "https://domain.com/callback.php";

$microsoft = new mAuth($tenant, $client_id,  $client_secret, $callback, $scopes);