定制 yosefa/microsoft-graph-api-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yosefa/microsoft-graph-api-php

最新稳定版本:1.0.3

Composer 安装命令:

composer require yosefa/microsoft-graph-api-php

包简介

Currently a simple utility for login using microsoft graph api php.

README 文档

README

Description

A lightweight PHP library for authenticating and retrieving user data from Microsoft Graph API using OAuth 2.0. This library simplifies the process of obtaining access tokens and fetching user information through Microsoft's authentication system.

Features

  • Easy Microsoft OAuth 2.0 authentication.
  • Token retrieval and management.
  • Fetch user profile information.
  • Supports multiple tenants and scopes.
  • Built with Guzzle HTTP client and Microsoft Graph SDK.

Prerequisites

  • PHP 7.4+
  • Composer
  • Microsoft Azure Active Directory application credentials

Installation

Install the library using Composer:

composer require yosefa/microsoft-graph-api-php

Configuration

  1. Create a Microsoft Azure AD application in the Azure Portal
  2. Obtain the following credentials:
    • Client ID
    • Client Secret
    • Tenant ID
    • Redirect URI

Usage Example

<?php
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();
}

Method Reference

Constructor

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

Methods

  • get_url_auth(): Generate Microsoft OAuth authorization URL
  • get_token($code): Exchange authorization code for access token
  • get_data($code): Retrieve authenticated user's profile information

Scopes

Default scope is User.Read. You can specify multiple scopes during initialization:

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

Error Handling

The library returns objects with a valid property:

  • true: Successful operation
  • false: Operation failed

Check the valid property and handle errors accordingly.

Dependencies

Contributing

Contributions are welcome! Please submit pull requests or open issues on the project repository.

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固