vgrem/php-spo
Composer 安装命令:
composer require vgrem/php-spo
包简介
PHP Office 365 library. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API
README 文档
README
Microsoft 365 Library for PHP. A REST/OData based client library for Microsoft 365.
Usage
- Installation
- Working with SharePoint API
- Working with Teams API
- Working with Outlook API
- Working with OneDrive API
Status
Installation
You can use Composer or simply Download the Release
Composer
The preferred method is via composer. Follow the installation instructions if you do not already have composer installed.
Once composer installed, execute the following command in your project root to install this library:
composer require vgrem/php-spo
or via composer.json file:
{
"require": {
"vgrem/php-spo": "^3"
}
}
Finally, be sure to include the autoloader:
require_once '/path/to/your-project/vendor/autoload.php';
Requirements
PHP version: PHP 7.1 or later
Working with SharePoint API
The list of supported SharePoint versions:
- SharePoint Online and OneDrive for Business
- SharePoint On-Premises (2013-2019)
Authentication
The following auth flows supported:
1. app principal with client credentials:
use Office365\Runtime\Auth\ClientCredential; use Office365\SharePoint\ClientContext; $credentials = new ClientCredential("{clientId}", "{clientSecret}"); $ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);
Documentation:
2. app principal with client certificate:
use Office365\Runtime\Auth\ClientCredential; use Office365\SharePoint\ClientContext; $tenant = "{tenant}.onmicrosoft.com"; //tenant id or name $privateKeyPath = "-- path to private.key file--" $privateKey = file_get_contents($privateKeyPath); $ctx = (new ClientContext("{siteUrl}"))->withClientCertificate( $tenant, "{clientId}", $privateKey, "{thumbprint}");
Documentation:
3. user credentials auth:
use Office365\Runtime\Auth\UserCredentials; use Office365\SharePoint\ClientContext; $credentials = new UserCredentials("{userName}", "{password}"); $ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);
4. NTLM auth (for SharePoint On-Premises):
use Office365\Runtime\Auth\UserCredentials; use Office365\SharePoint\ClientContext; $credentials = new UserCredentials("{userName}", "{password}"); $ctx = (new ClientContext("{siteUrl}"))->withNtlm($credentials);
Examples
The following examples demonstrates how to perform basic CRUD operations against SharePoint list item resources:
Example 1. How to read SharePoint list items:
use Office365\SharePoint\ClientContext; use Office365\Runtime\Auth\ClientCredential; use Office365\SharePoint\ListItem; $credentials = new ClientCredential("{client-id}", "{client-secret}"); $client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials); $web = $client->getWeb(); $list = $web->getLists()->getByTitle("{list-title}"); //init List resource $items = $list->getItems(); //prepare a query to retrieve from the $client->load($items); //save a query to retrieve list items from the server $client->executeQuery(); //submit query to SharePoint server /** @var ListItem $item */ foreach($items as $item) { print "Task: {$item->getProperty('Title')}\r\n"; }
or via fluent API syntax:
use Office365\SharePoint\ClientContext; use Office365\Runtime\Auth\ClientCredential; use Office365\SharePoint\ListItem; $credentials = new ClientCredential("{client-id}", "{client-secret}"); $client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials); $items = $client->getWeb() ->getLists() ->getByTitle("{list-title}") ->getItems() ->get() ->executeQuery(); /** @var ListItem $item */ foreach($items as $item) { print "Task: {$item->getProperty('Title')}\r\n"; }
Example 2. How to create SharePoint list item:
use Office365\SharePoint\ClientContext; use Office365\Runtime\Auth\ClientCredential; $credentials = new ClientCredential("{client-id}", "{client-secret}"); $client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials); $list = $client->getWeb()->getLists()->getByTitle("Tasks"); $itemProperties = array('Title' => 'Order Approval', 'Body' => 'Order approval task'); $item = $list->addItem($itemProperties)->executeQuery(); print "Task {$item->getProperty('Title')} has been created.\r\n";
Example 3. How to delete a SharePoint list item:
use Office365\SharePoint\ClientContext; use Office365\Runtime\Auth\ClientCredential; $credentials = new ClientCredential("{client-id}", "{client-secret}"); $client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials); $list = $client->getWeb()->getLists()->getByTitle("Tasks"); $listItem = $list->getItemById("{item-id-to-delete}"); $listItem->deleteObject()->executeQuery();
Example 4. How to update SharePoint list item:
use Office365\SharePoint\ClientContext; use Office365\Runtime\Auth\ClientCredential; $credentials = new ClientCredential("{client-id}", "{client-secret}"); $client = (new ClientContext("https://{your-tenant-prefix}.sharepoint.com"))->withCredentials($credentials); $list = $client->getWeb()->getLists()->getByTitle("Tasks"); $listItem = $list->getItemById("{item-id-to-update}"); $listItem->setProperty('PercentComplete',1); $listItem->update()->executeQuery();
Working with Teams API
Example: create a Team
The following is an example of a minimal request to create a Team (via delegated permissions)
use Office365\GraphServiceClient; use Office365\Runtime\Auth\AADTokenProvider; use Office365\Runtime\Auth\UserCredentials; function acquireToken() { $tenant = "{tenant}.onmicrosoft.com"; $resource = "https://graph.microsoft.com"; $provider = new AADTokenProvider($tenant); return $provider->acquireTokenForPassword($resource, "{clientId}", new UserCredentials("{UserName}", "{Password}")); } $client = new GraphServiceClient("acquireToken"); $teamName = "My Sample Team"; $newTeam = $client->getTeams()->add($teamName)->executeQuery();
Working with Outlook API
Supported list of APIs:
The following example demonstrates how to send a message via Outlook Mail API:
use Office365\GraphServiceClient; use Office365\Outlook\Message; use Office365\Outlook\ItemBody; use Office365\Outlook\BodyType; use Office365\Outlook\EmailAddress; use Office365\Runtime\Auth\AADTokenProvider; use Office365\Runtime\Auth\UserCredentials; function acquireToken() { $tenant = "{tenant}.onmicrosoft.com"; $resource = "https://graph.microsoft.com"; $provider = new AADTokenProvider($tenant); return $provider->acquireTokenForPassword($resource, "{clientId}", new UserCredentials("{UserName}", "{Password}")); } $client = new GraphServiceClient("acquireToken"); /** @var Message $message */ $message = $client->getMe()->getMessages()->createType(); $message->setSubject("Meet for lunch?"); $message->setBody(new ItemBody(BodyType::Text,"The new cafeteria is open.")); $message->setToRecipients([new EmailAddress(null,"fannyd@contoso.onmicrosoft.com")]); $client->getMe()->sendEmail($message,true)->executeQuery();
Working with OneDrive API
The following example demonstrates how retrieve My drive Url via OneDrive API:
use Office365\GraphServiceClient; use Office365\Runtime\Auth\AADTokenProvider; use Office365\Runtime\Auth\UserCredentials; function acquireToken() { $tenant = "{tenant}.onmicrosoft.com"; $resource = "https://graph.microsoft.com"; $provider = new AADTokenProvider($tenant); return $provider->acquireTokenForPassword($resource, "{clientId}", new UserCredentials("{UserName}", "{Password}")); } $client = new GraphServiceClient("acquireToken"); $drive = $client->getMe()->getDrive()->get()->executeQuery(); print $drive->getWebUrl();
vgrem/php-spo 适用场景与选型建议
vgrem/php-spo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.51M 次下载、GitHub Stars 达 375, 最近一次更新时间为 2016 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curl」 「rest」 「api」 「Office365」 「microsoftgraph」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vgrem/php-spo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vgrem/php-spo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vgrem/php-spo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
cURL and cURL based Soap-Client for PHP
Simple cURL wrapper
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
A fluent PHP CURL wrapper
Build mini web servers in PHP for testing
统计信息
- 总下载量: 1.51M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 377
- 点击次数: 30
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-21