源自vgrem/phpSPO

v2.2.2 2018-10-27 19:00 UTC

This package is auto-updated.

Last update: 2024-09-24 19:42:24 UTC


README

关于

该库为PHP应用程序提供了一个Office 365 REST客户端。它允许通过基于REST/OData的API对Office 365资源执行CRUD操作。

支持的Office 365 REST API列表

状态

Total Downloads Latest Stable Version Build Status License

PayPal

安装

您可以使用Composer或简单地下载发行版

Composer

首选方法是使用composer。如果您尚未安装composer,请遵循安装说明

一旦安装了composer,请在您的项目根目录中执行以下命令以安装此库

composer require vgrem/php-spo:dev-master -n --no-progress

最后,请确保包含自动加载器

require_once '/path/to/your-project/vendor/autoload.php';

PHP版本

API

  • PHP:cURL底层库用于执行HTTP请求
  • ClientContext - 表示SharePoint客户端上下文,通过SharePoint Online REST API对SharePoint资源执行CRUD操作
  • OutlookClient - 表示客户端上下文,通过执行CRUD操作对Office资源(如Outlook资源)执行操作
  • ClientRequest - 表示客户端请求(比ClientContext更底层),通过SharePoint Online REST API对SharePoint资源执行CRUD操作
  • AuthenticationContext - 表示一个对象,提供访问SharePoint Online资源的凭据。
  • NetworkCredentialContext - 为基于密码的身份验证方案(如基本身份验证)提供凭据。

有两种方法可用于执行基于REST的查询

  • 通过ClientRequest类,您需要通过指定端点URL、所需的头信息和有效负载来构建REST查询(低级方法),请参阅renameFolder.php以获取更多详细信息
  • 通过ClientContext类,您针对客户端对象资源,如Web、ListItem等,请参阅list_examples.php以获取更多详细信息

用法

使用SharePoint REST API

以下示例演示了如何对SharePoint列表项资源执行基本的CRUD操作。

示例1. 如何读取SharePoint列表项

$authCtx = new AuthenticationContext($Url);
$authCtx->acquireTokenForUser($UserName,$Password); //authenticate

$ctx = new ClientContext($Url,$authCtx); //initialize REST client    
$web = $ctx->getWeb();
$list = $web->getLists()->getByTitle($listTitle); //init List resource
$items = $list->getItems();  //prepare a query to retrieve from the 
$ctx->load($items);  //save a query to retrieve list items from the server 
$ctx->executeQuery(); //submit query to SharePoint Online REST service
foreach( $items->getData() as $item ) {
    print "Task: '{$item->Title}'\r\n";
}

示例2. 如何创建SharePoint列表项

$listTitle = 'Tasks';
$list = $ctx->getWeb()->getLists()->getByTitle($listTitle);
$itemProperties = array('Title' => 'Order Approval', 'Body' => 'Order approval task','__metadata' => array('type' => 'SP.Data.TasksListItem'));
$item = $list->addItem($itemProperties);
$ctx->executeQuery();
print "Task '{$item->Title}' has been created.\r\n";

示例3. 如何删除SharePoint列表项

$listTitle = 'Tasks';
$itemToDeleteId = 1;
$list = $ctx->getWeb()->getLists()->getByTitle($listTitle);
$listItem = $list->getItemById($itemToDeleteId);
$listItem->deleteObject();
$ctx->executeQuery();

示例4. 如何更新SharePoint列表项

$listTitle = 'Tasks';
$itemToUpdateId = 1;
$list = $ctx->getWeb()->getLists()->getByTitle($listTitle);
$listItem = $list->getItemById($itemId);
$listItem->setProperty('PercentComplete',1);
$listItem->update();
$ctx->executeQuery();

使用Outlook REST API

以下示例演示了如何通过Outlook邮件API读取、创建和发送邮件。

示例1. 如何创建草稿邮件

$authCtx = new NetworkCredentialContext($UserName,$Password); //using Basic Auth scheme (for v1 API only)
$ctx = new OutlookClient($authCtx); //initialize OutlookServices client
$message = $ctx->getMe()->getMessages()->createMessage(); //create a Message resource
//set Message properties
$message->Subject = "--subject--";
$message->Body = new ItemBody(BodyType::Text,"--Content goes here--");
$message->ToRecipients = array(
     new Recipient(new EmailAddress("Jon Doe","jdoe@contoso.onmicrosoft.com"))
);
$ctx->executeQuery();

示例2. 如何获取邮件

$authCtx = new NetworkCredentialContext($UserName,$Password); //using Basic Auth scheme (for v1 API only)
$ctx = new OutlookClient($authCtx); //initialize OutlookServices client
$messages = $ctx->getMe()->getMessages();
$ctx->load($messages);
$ctx->executeQuery();
//print messages subjects
foreach ($messages->getData() as $curMessage){
   print $curMessage->Subject;
}

示例3. 如何发送邮件

$authCtx = new NetworkCredentialContext($UserName,$Password); //using Basic Auth scheme (for v1 API only)
$ctx = new OutlookClient($authCtx); //initialize OutlookServices client
$message = $ctx->getMe()->getMessages()->createMessage(); //create a Message resource
//set Message properties
$message->Subject = "--subject--";
$message->Body = new ItemBody(BodyType::Text,"--Content goes here--");
$message->ToRecipients = array(
     new Recipient(new EmailAddress("Jon Doe","jdoe@contoso.onmicrosoft.com"))
);
$ctx->getMe()->sendEmail($message,false); //send a Message
$ctx->executeQuery();

变更日志

1.0.0 - 2014年5月23日

  • 初始发布。

2.0.0 - 2016年2月14日

  • 引入了AuthenticationContextClientContext类。