plai2010/php-msgraph
Composer 安装命令:
composer require plai2010/php-msgraph
包简介
Microsoft Graph utilities for PHP.
README 文档
README
This is a utility package for using Microsoft Graph API in PHP
applications. In this version it only supports sending email
messages to the sendMail endpoint.
The use case for this package was email notification in a Laravel (10.x) application. A Laravel service provider is included.
This package is designed to work with [plai2010/php-oauth2][plai2010-oauth2] for OAuth2 token management.
Installation
The package can be installed from Packagist:
$ composer require plai2010/php-msgraph
One may also clone the source repository from Github:
$ git clone https://github.com/plai2010/php-msgraph.git
$ cd php-msgraph
$ composer install
Example: Sending Email
This package works best as a Laravel extension. It is possible to use it as just a utility library, but as one would see in this example, it can be a little awkard.
use PL2010\MsGraph\MsGraphClient;
use PL2010\OAuth2\Contracts\TokenRepository;
// Some PL2010\OAuth2\Contracts\TokenRepository.
// Here we have a single hard coded access token
// wrapped as a token repository.
$tkrepo = new class implements TokenRepository {
public function getOAuth2Token(string $key, int $valid=0): array {
return [
// One single hard coded token regardless of $key.
'access_token' => '....',
];
}
public function putOAuth2Token(string $key, array $token): static {
return $this;
}
};
$msgraph = new MsGraphClient('sendmail', [
// OAuth2 access token will be retrieved from the
// token repository using this key.
'token_key' => 'does-not-matter',
], [
// Provide token repository.
'token_repo' => $tkrepo,
]);
$msg = (new \Symfony\Component\Mime\Email())
->from('no-reply@example.com')
->to('john.doe@example.com')
->subject('hello')
->html('<h3>Greetings!</h3>')
->attach(file_get_contents('wave.png'), 'wave.png', 'image/png')
;
$msgraph->sendEmail($msg);
Client Manager
An application typically acts as a single MS Graph API client.
MsGraphManager allows multiple
clients to be configured for more complex scenarios. For example,
there may a client that acts for the application, and another
as agent for individual users.
// Create manager.
$manager = new PL2010\MsGraph\MsGraphManager;
// Configure a application/service level client.
$manager->configure('service', [
'token_key' => 'msgraph:app',
]);
// Configure an agent client. Here we assume that
// the token repository recognizes the '@user' tag
// in the token key and looks up user specific
// access token.
$manager->configure('agent', [
'token_key' => 'msgraph:agent@user',
]);
Laravel Integration
This package includes a Laravel service provider that makes
a singleton MsGraphManager available
two ways:
* Abstract 'msgraph' in the application container, i.e. `app('msgraph')`.
* Facade alias 'MsGraph', i.e. `MsGraph::`.
The configuration file is config/msgraph.php. It returns an
associative array of client configurations by names, like this:
<?php
return [
'msgraph_site' => [
'token_key' => 'msgraph:app',
],
'msgraph_ua' => [
'token_key' => 'msgraph:agent@user',
],
...
];
Also a 'msgraph' email transport is registered. So one may define a
mailer in config/mailer.php like this:
'outlook' => [
'transport' => 'msgraph',
// Points to entry in config/msgraph.php.
'msgraph' => 'msgraph_site',
]
A message can then be sent like this:
use Illuminate\Mail\Mailable;
$msg = (new Mailable())
->to('john.doe@example.com')
->subject('hello')
->html('<h3>Greetings!</h3>')
->send(Mail::mailer('outlook'));
plai2010/php-msgraph 适用场景与选型建议
plai2010/php-msgraph 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 248 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 plai2010/php-msgraph 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 plai2010/php-msgraph 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 248
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-24