2017 © Pedro Peláez
 

library php-spo

The library provides a Office 365 REST client for PHP. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API

image

vgrem/php-spo

The library provides a Office 365 REST client for PHP. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API

  • Tuesday, July 17, 2018
  • by muffe
  • Repository
  • 17 Watchers
  • 97 Stars
  • 15,430 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 39 Forks
  • 39 Open issues
  • 4 Versions
  • 23 % Grown

The README.md

About

Microsoft 365 Library for PHP. A REST/OData based client library for Microsoft 365., (*1)

Usage

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

Status

Total Downloads Latest Stable Version Build Status License, (*2)

Installation

You can use Composer or simply Download the Release, (*3)

Composer

The preferred method is via composer. Follow the installation instructions if you do not already have composer installed., (*4)

Once composer installed, execute the following command in your project root to install this library:, (*5)

composer require vgrem/php-spo

or via composer.json file:, (*6)

{
    "require": {
        "vgrem/php-spo": "^3"
    }
}

Finally, be sure to include the autoloader:, (*7)

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

Requirements

PHP version: PHP 7.1 or later, (*8)

Working with SharePoint API

The list of supported SharePoint versions:, (*9)

  • SharePoint Online and OneDrive for Business
  • SharePoint On-Premises (2013-2019)

Authentication

The following auth flows supported:, (*10)

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:, (*11)

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:, (*12)

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:, (*13)

Example 1. How to read SharePoint list items:, (*14)

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:, (*15)


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:, (*16)

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:, (*17)

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:, (*18)

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), (*19)


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:, (*20)

The following example demonstrates how to send a message via Outlook Mail API:, (*21)

 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:, (*22)

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();

The Versions

17/07 2018

dev-master

9999999-dev

The library provides a Office 365 REST client for PHP. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.4
  • ext-curl *

 

by Vadim Gremyachev

api curl rest office365 sharepoint

17/07 2018

v2.2.0

2.2.0.0

The library provides a Office 365 REST client for PHP. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.4
  • ext-curl *

 

by Vadim Gremyachev

api curl rest office365 sharepoint

29/06 2018

v2.1.9

2.1.9.0

The library provides a Office 365 REST client for PHP. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.4
  • ext-curl *

 

by Vadim Gremyachev

api curl rest office365 sharepoint

20/04 2017

v2.1.8

2.1.8.0

The library provides a Office 365 REST client for PHP. It allows to performs CRUD operations against Office 365 resources via an REST/OData based API

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.4
  • ext-curl *

 

by Vadim Gremyachev

api curl rest office365 sharepoint