PHPackages                             zatxm/microsoft-entra - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. zatxm/microsoft-entra

ActiveLibrary[HTTP &amp; Networking](/categories/http)

zatxm/microsoft-entra
=====================

PHP version of simple microsoft entra application,including oauth2 login authentication to obtain token,api request

1.3.0(8mo ago)41.3kMITPHPPHP &gt;=7.1

Since Oct 18Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/zatxm/microsoft-entra)[ Packagist](https://packagist.org/packages/zatxm/microsoft-entra)[ RSS](/packages/zatxm-microsoft-entra/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (6)Used By (0)

Microsoft Entra Sample Api Request
==================================

[](#microsoft-entra-sample-api-request)

php版简单microsoft entra应用，包括oauth2登录鉴权获取token，api请求

Usage
-----

[](#usage)

composer require zatxm/microsoft-entra

1、请求授权代码

```
/**
 * 请求授权代码
 */
use Zatxm\MicrosoftEntra\OAuth;

$oauth = new OAuth([
    'clientId' => 'xxxx', //应用程序ID
    'clientSecret' => 'xxxx', //应用程序秘钥
    'scope'        => 'openid profile offline_access user.read', //权限内容
    'oauthRedirectUri' => 'http://localhost:9003', //配置的回调地址
    // 鉴权接口地址,默认为以下三个可以不传
    'oauthAuthority'   => 'https://login.microsoftonline.com/common',
    'oauthAuthorizeEndpoint' => '/oauth2/v2.0/authorize',
    'oauthTokenEndpoint'     => '/oauth2/v2.0/token'
]);
// 应该执行跳转此url，它会要求你登录、授权并跳转到回调url
// 跳转到回调url，您应该验证state，state可以通过下面获取
// 回调会返回code，一般10分钟有效，您应该用它来获取token
// 您应该可以存储code和以下的tag，因为他们可用与获取token
$authUrl = $oauth->getAuthorizationUrl();
// tag会返回如['state'=>'xx', 'codeVerifier'=>'xx', 'codeChallenge'=>'xx'];
$tag = $oauth->getTag();
```

2、获取access token

```
/**
 * 获取access token
 */
use Zatxm\MicrosoftEntra\OAuth;

$oauth = new OAuth([
    'clientId' => 'xxxx', //应用程序ID
    'clientSecret' => 'xxxx', //应用程序秘钥
    'scope'        => 'openid profile offline_access user.read', //权限内容
    'oauthRedirectUri' => 'http://localhost:9003', //配置的回调地址
]);
// 微软回调返回的code值，有效期一般10分钟
$authorizationCode = 'xx';
// 可以通过上步的tag取得
$codeVerifier = 'xx';
$accessTokenArr = $oauth->getAccessToken($authorizationCode, $codeVerifier);
$accessToken = $accessTokenArr['access_token']; //可能有error错误码
```

3、请求entra应用api如graph

```
/**
 * 发送邮件
 */
use Zatxm\MicrosoftEntra\EntraApi;

$accessToken = 'xx'; //上步获取的access token
$graphApiEndpoint = 'https://graph.microsoft.com/v1.0'; //接口地址，可以不传默认就是这个
$api = new EntraApi($accessToken, $graphApiEndpoint);
$api->setAccessToken('xxxx'); //可以设置新的token
$params = [
    'message' => [
        'subject' => '邮件标题',
        'body'    => [
            'contentType' => 'html',
            'content'     => '邮件内容'
        ],
        'toRecipients' => [['emailAddress'=>['address'=>'xxxx']]]
    ],
    'saveToSentItems' => true
];
$res = $api->go('/me/sendMail', $params, 'POST');
```

4、access token过期可以通过刷新token重新获取

```
/**
 * 刷新获取access token
 */
use Zatxm\MicrosoftEntra\OAuth;

$oauth = new OAuth([
    'clientId' => 'xxxx', //应用程序ID
    'clientSecret' => 'xxxx', //应用程序秘钥
    'scope'        => 'openid profile offline_access user.read', //权限内容
    'oauthRedirectUri' => 'http://localhost:9003', //配置的回调地址
]);
$refreshToken = 'xx'; //refresh_token
$accessTokenArr = $oauth->getAccessTokenRefresh($refreshToken);
$accessToken = $accessTokenArr['access_token']; //可能有error错误码
```

License
-------

[](#license)

MIT

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance62

Regular maintenance activity

Popularity22

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~173 days

Total

5

Last Release

243d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/261b444d45e7c3985e763197811f598d2ae49ce7727fd5e9970fdd92532a40ad?d=identicon)[zatxm](/maintainers/zatxm)

---

Top Contributors

[![zatxm](https://avatars.githubusercontent.com/u/66353899?v=4)](https://github.com/zatxm "zatxm (7 commits)")

---

Tags

httpmicrosoftgraphoauth2.0microsoft entra

### Embed Badge

![Health badge](/badges/zatxm-microsoft-entra/health.svg)

```
[![Health](https://phpackages.com/badges/zatxm-microsoft-entra/health.svg)](https://phpackages.com/packages/zatxm-microsoft-entra)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.0B3.2k](/packages/guzzlehttp-psr7)[psr/http-message

Common interface for HTTP messages

7.1k1.0B5.5k](/packages/psr-http-message)[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k692.9M1.9k](/packages/psr-http-factory)[psr/link

Common interfaces for HTTP links

2.5k144.1M69](/packages/psr-link)[rmccue/requests

A HTTP library written in PHP, for human beings.

3.6k34.5M257](/packages/rmccue-requests)[league/uri

URI manipulation library

1.1k206.4M277](/packages/league-uri)

PHPackages © 2026

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