PHPackages                             smileandlearn/microsoft-api-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. smileandlearn/microsoft-api-php

ActiveLibrary[API Development](/categories/api)

smileandlearn/microsoft-api-php
===============================

Library to connect external login to Microsoft Azure

1.0.1(3y ago)117MITPHPPHP &gt;=5.6

Since Oct 18Pushed 3y ago1 watchersCompare

[ Source](https://github.com/elrincondeisma/smileandlearn-microsoft-api)[ Packagist](https://packagist.org/packages/smileandlearn/microsoft-api-php)[ Docs](https://wwww.smileandlearn.com)[ RSS](/packages/smileandlearn-microsoft-api-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

Microsoft API PHP Wrapper
=========================

[](#microsoft-api-php-wrapper)

Microsoft provides its application registration via its server side of giant service service called Azure. In order to use its API and this wrapper you must need to register your own Microsoft App in Azure Portal.

### Installation

[](#installation)

You can install this library to your project via composer using the following command:

`composer require smileandlearn/microsoft-api-php`

### Authentication

[](#authentication)

Microsoft uses OAuth 2.0 meaning Access Token is the key to pull data from the Microsoft servers on behalf of the user for which access token is generated. Access token is generated only for the nodes for which the authenticated user allowed your app to.

These nodes are called as scopes which dictates what types of data and functions can the app perform on the behalf of the authenticated user (against her access token). You can get the applicable scopes, in this [document](https://docs.microsoft.com/en-us/graph/permissions-reference "document").

This wrapper also supports tenants. If you want users of only your organization (Azure) to use this app, you can use respective tenant otherwise common will work fine for general microsoft user authentication.

```
session_start(); // Important
require "vendor/autoload.php";

use Smileandlearn\Microsoft\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);

```

### Fetching &amp; Setting Access Token (Shorter life)

[](#fetching--setting-access-token-shorter-life)

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

```
// ON CALLBACK
session_start();
require "vendor/autoload.php";

use Smileandlearn\Microsoft\mAuth;
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);

```

### Fetching user data

[](#fetching-user-data)

Once the access token is set to the wrapper, you can just initialize the User model and the related data will get pulled out for you.

```
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(); // adnanhussainturki@gmail.com

```

### Fetching Refresh Token (Longer Life)

[](#fetching-refresh-token-longer-life)

For fetching refresh token along with the access token, you need to provide `offline_access` as an scope and then fetch access token. On the callback, you will recieve refresh token along with access token.

```
// ON CALLBACK
session_start();
require "vendor/autoload.php";

use Smileandlearn\Microsoft\mAuth;
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;

```

### Using Refresh Token

[](#using-refresh-token)

If you have active refresh token for an user, you do need ask for the user permission to get accesss token against that user. You can set the refresh token to the wrapper and the wrapper will fetch access token for you against it.

```
$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(); // adnanhussainturki@gmail.com

```

### License

[](#license)

Licensed under Apache 2.0. You can check its details [here](https://choosealicense.com/licenses/apache-2.0/ "here").

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1308d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/563587ace0036d820041d60e4de756d999d1be234d696fa9a66bf6449606f04a?d=identicon)[ismael.catala](/maintainers/ismael.catala)

---

Tags

apiOneDrive

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/smileandlearn-microsoft-api-php/health.svg)

```
[![Health](https://phpackages.com/badges/smileandlearn-microsoft-api-php/health.svg)](https://phpackages.com/packages/smileandlearn-microsoft-api-php)
```

###  Alternatives

[adnanhussainturki/microsoft-api-php

PHP Wrapper the Microsoft Graph API

2760.1k](/packages/adnanhussainturki-microsoft-api-php)[m165437/laravel-blueprint-docs

API Blueprint Renderer for Laravel

22779.0k](/packages/m165437-laravel-blueprint-docs)[lovattj/php-skydrive

A PHP client library for Microsoft SkyDrive/OneDrive

3923.1k](/packages/lovattj-php-skydrive)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
