Office 365 Library for PHP. A REST/OData based client library for Office 365.
You can use Composer or simply Download the Release
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';
PHP version: PHP 5.4 or later
The list of supported SharePoint versions:
- SharePoint Online and OneDrive for Business
- SharePoint On-Premises (2013-2019)
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);
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();
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,"[email protected]"))
);
$client->getMe()->sendEmail($message,true);
$client->executeQuery();
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");