SharePoint Online client for PHP

In this post I would like to introduce you to a SharePoint client for PHP available on GitHub. The library provides a SharePoint Online (SPO) client for PHP applications. It allows you to performs CRUD operations on SharePoint data using an SharePoint 2013 REST/OData based API.

 API

PHP:cURL library is used to make HTTP requests to performs CRUD operations on SharePoint data using SharePoint 2013 REST API.

Getting started

A first example

<?php
$username = 'username@tenant.onmicrosoft.com';
$password = 'password';
$url = "https://tenant.sharepoint.com/&quot;;
try {
$authCtx = new AuthenticationContext($Url);
$authCtx->acquireTokenForUser($username,$password);
$ctx = new ClientContext($Url,$authCtx);
printTasks($ctx);
}
catch (Exception $e) {
echo 'Authentication failed: ', $e->getMessage(), "\n";
}
function printTasks(ClientContext $ctx){
$listTitle = 'Tasks';
$web = $ctx->getWeb();
$list = $web->getLists()->getByTitle($listTitle);
$items = $list->getItems();
$ctx->load($items);
$ctx->executeQuery();
foreach( $items->getData() as $item ) {
print "Task: '{$item->Title}'\r\n";
}
}
?>

view raw
SPOFirstExample.php
hosted with ❤ by GitHub

Key points:

  • SPOClient class represents a REST Service client for the specified SharePoint Online (SPO) site
  • The signIn method performs a claims-based authentication. You can find more details about remote authentication in Remote authentication in SharePoint Online post.

The following examples demonstrates how to perform the remaining CRUD operations on SharePoint list data.

<?php
require_once 'SPOClient.php';
$username = 'username@tenant.onmicrosoft.com';
$password = 'password';
$url = "https://tenant.sharepoint.com/&quot;;
$client = new SPOClient($url);
$client->signIn($username,$password);
//Get Tasks list
$listTitle = 'Tasks';
$list = $client->getList($listTitle);
//Create a Task item
$itemProperties = array('Title' => 'Order Approval', 'Body' => 'Order approval task');
$taskItem = $list->addItem($itemProperties);
print "Task '{$taskItem->Title}' has been created succesfully.\r\n";
$itemId = $taskItem->Id;
//Update a Task item
$itemProperties = array('PercentComplete' => 1);
$list->updateItem($itemId,$itemProperties);
//Delete a Task item
$list->deleteItem($itemId);
?>

view raw
ListExamples.php
hosted with ❤ by GitHub

 List data generator

And finally one more example that demonstrates how to populate Contacts list in SharePoint. For generating fake data we will utilize Faker PHP library, below is provided the complete example:

<?php
require_once __DIR__ . '/../../Faker/src/autoload.php'; //Faker library (https://github.com/fzaninotto/Faker)
require_once 'SPOClient.php';
$username = 'username@tenant.onmicrosoft.com';
$password = 'password';
$url = "https://tenant.sharepoint.com/&quot;;
generateContacts($url,$username,$password);
function generateContacts($url,$username,$password){
$client = new SPOClient($url);
$client->signIn($username,$password);
$list = $client->getList('Contacts');
$contactsCount = 120;
for($i = 0; $i < $contactsCount; $i++){
$contactEntry = createContactEntry();
$item = $list->addItem($contactEntry);
print "Contact '{$item->Title}' has been created succesfully.\r\n";
}
}
function createContactEntry()
{
$contactCard = Faker\Factory::create();
return array('Title' => $contactCard->username,
'FullName' => $contactCard->name,
'Email' => $contactCard->email,
'Company' => $contactCard->company,
'WorkPhone' => $contactCard->phoneNumber,
'WorkAddress' => $contactCard->streetAddress,
'WorkCity' => $contactCard->city,
'WorkZip' => $contactCard->postcode,
'WorkCountry' => $contactCard->country,
'WebPage' => array ('Url' => $contactCard->url)
);
}
?>

Summary

The library currently supports CRUD operations against SharePoint list data. But since SharePoint 2013 REST covers a much broader set of API, the library could be extended.

Hopefully this helps you get started how you can interact with a SharePoint Online site from a remote Web application written in PHP!