andrewsauder/microsoft-services

用于简化Microsoft Graph服务实现的PHP包装器

v1.4.2 2023-12-21 13:32 UTC

This package is auto-updated.

Last update: 2024-09-28 21:54:52 UTC


README

用于简化Microsoft Graph服务实现的PHP包装器

要求

版本 >=1.2 需要 PHP >=8.1

安装

composer require andrewsauder/microsoft-services

服务配置

$config = new \andrewsauder\microsoftServices\config();
$config->clientId = '{Azure Application ID}';
$config->clientSecret = '{Azure Client Secret}';  //certificates not yet supported
$config->tenant = 'example.com';
$config->driveId = '';                            //required if using the files service - cay be found using Graph explorer
$config->fromAddress = 'noreply@example.com';     //required if using mail service - this is just a default

文件使用

获取文件列表

从根目录

$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$files = $microsoftFiles->list();

从子目录

//example subdirectory: {root}/2021-0001/Building 1/Inspections
$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$files = $microsoftFiles->list( [ '2021-0001', 'Building 1', 'Inspections' ] );

上传文件

到根目录

$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$uploadFileResponse = $microsoftFiles->upload( 'C:\tmp\testFile.txt', 'testFile.txt' );

到子目录

//example subdirectory: {root}/2021-0001/Building 1/Inspections
$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$uploadFileResponse = $microsoftFiles->upload( 'C:\tmp\testFile.txt', 'testFile.txt', [ '2021-0001', 'Building 1', 'Inspections' ] );

删除文件

$microsoftFiles = new \andrewsauder\microsoftServices\files( $config );
$deleteResponse = $microsoftFiles->delete( $itemId );

邮件使用

如果没有提供用户令牌,将使用应用程序令牌。

如果正在使用应用程序令牌,请验证Azure应用程序是否有正确的Mail.X权限来使用电子邮件地址。要限制应用程序仅对某些邮箱的访问,请使用ExchangeOnline Powershell应用访问策略。更多信息请参阅https://learn.microsoft.com/en-us/powershell/module/exchange/new-applicationaccesspolicy?view=exchange-ps

当创建服务时(在mail($config, 'user-access-token-string')中提供用户访问令牌时),请验证用户在Office 365中正确配置了“代表发送”或“以发送者身份发送”权限。

发送电子邮件

如果没有提供发件人地址,将使用配置中的默认发件人地址。

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$microsoftMail->addAttachment( 'C:\tmp\testFile.txt' );
$rsp = $microsoftMail->send( 'to@example.com', 'Subject', 'HTML compatible message', 'from@example.com' );

if( $rsp->getStatus() < 200 || $rsp->getStatus() >= 300 ) {
    error_log( 'Failed' );
}

获取所有消息

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$messages = $microsoftMail->getAllMessages( 'joeschmoe@example.com' );

从特定文件夹获取所有消息

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$messages = $microsoftMail->getMessagesInFolder( 'joeschmoe@example.com', 'mail-folder-id' );

获取所有文件夹

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$folders = $microsoftMail->getFolders( 'joeschmoe@example.com' );

获取消息的附件

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$attachments = $microsoftMail->getAttachments( 'joeschmoe@example.com', 'message-id' );

删除消息

$microsoftMail = new \andrewsauder\microsoftServices\mail( $config );
$graphResponse = $microsoftMail->deleteMessage( 'joeschmoe@example.com', 'message-id' );

用户使用

获取组织中的所有用户

$microsoftUserService = new \andrewsauder\microsoftServices\user( $config );
$users = $microsoftUserService->allUsersInOrganization();

通过用户主体名称获取用户

$microsoftUserService = new \andrewsauder\microsoftServices\user( $config );
$users = $microsoftUserService->getUserByUserPrincipalName( 'andrew@sauder.software' );

通过ID获取用户

$microsoftUserService = new \andrewsauder\microsoftServices\user( $config );
$users = $microsoftUserService->getUserById( '15bd6895-bf60-4125-a1d2-affb7e0de5d8' );

通过高级筛选器获取用户

有关筛选器详情,请参阅https://learn.microsoft.com/en-us/graph/filter-query-parameter?tabs=http

$microsoftUserService = new \andrewsauder\microsoftServices\user( $config );
$users = $microsoftUserService->getUsersByFilter( 'startswith(userPrincipalName,"andrew")' );