/phpSPO

Office 365 Library for PHP. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API

Primary LanguagePHPMIT LicenseMIT

About

Office 365 Library for PHP. A REST/OData based client library for Office 365.

Usage

  1. Installation
  2. Working with SharePoint API
  3. Working with Outlook API
  4. Working with OneDrive API

Status

Total Downloads Latest Stable Version Build Status License

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 is 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": "^2.3"
    }
}

Finally, be sure to include the autoloader:

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

Requirements

PHP version: PHP 5.4 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 are supported:

  • app principals (client credentials) auth (refer Granting access using SharePoint App-Only for a details):

     $authCtx = new AuthenticationContext($url);
     $authCtx->acquireAppOnlyAccessToken($clientId,$clientSecret);
     $ctx = new ClientContext($url,$authCtx);
    
  • user credentials auth:

    
    $authCtx = new AuthenticationContext($url);
    $authCtx->acquireTokenForUser($username,$password);
    $ctx = new ClientContext($url,$authCtx);
    
    
  • NTLM auth (for SharePoint On-Premises):

     $authCtx = new NetworkCredentialContext($username, $password);
     $authCtx->AuthType = CURLAUTH_NTLM;
     $ctx = new ClientContext($url,$authCtx);
    

Examples

The following examples demonstrates how to perform basic CRUD operations against SharePoint list item resources:

Example 1. How to read SharePoint list items


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

$ctx = new ClientContext($Url,$authCtx);     
$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";
}

Example 2. How to create SharePoint list item:

$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";

Example 3. How to delete a SharePoint list item:

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

Example 4. How to update SharePoint list item:

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

Working with Outlook API

Supported list of APIs:

The following example demonstrates how to send a message via Outlook Mail API:


 $client = new OutlookClient($settings['TenantName'],function (AuthenticationContext $authCtx) {        
        $authCtx->setAccessToken("--access token goes here--");
 });
 $message = $client->getMe()->getMessages()->createMessage();
 $message->Subject = "Meet for lunch?";
 $message->Body = new ItemBody(BodyType::Text,"The new cafeteria is open.");
 $message->ToRecipients = array(
     new Recipient(new EmailAddress(null,"vgrem@mediadev8.onmicrosoft.com"))
 );
 $client->getMe()->sendEmail($message,true);
 $client->executeQuery();


Working with OneDrive API

The following example demonstrates how retrieve my drive Url via OneDrive API:


$client = new GraphServiceClient($settings['TenantName'],function (AuthenticationContext $authCtx) use($settings) {
      $authCtx->setAccessToken("--access token goes here--");
});

$drive = $client->getMe()->getDrive();
$client->load($drive);
$client->executeQuery();
print $drive->getProperty("webUrl");