Install phpspo via Composer
masterInstall the Microsoft 365 library using Composer. This is the preferred method. Ensure you have PHP 7.1 or later installed.
composer require vgrem/php-sporepository·master·Indexed 18 days ago
https://github.com/vgrem/phpspoA REST/OData based PHP client library for interacting with Microsoft 365 services, including SharePoint, Teams, Outlook, and OneDrive. It supports multiple authentication methods including Client Credentials, Client Certificates, User Credentials, and NTLM for SharePoint On-Premises. The library provides capabilities for CRUD operations on SharePoint list items, managing Microsoft Teams via Graph API, sending emails via Outlook Mail API, and interacting with Microsoft 365 directory entities such as Applications, Persons, and Sign-In activities.
Install the Microsoft 365 library using Composer. This is the preferred method. Ensure you have PHP 7.1 or later installed.
composer require vgrem/php-spoTo use this project, ensure the following PHP extensions are enabled in your environment:
On Ubuntu/Linux, you can install them using apt-get:
apt-get install php-curl
apt-get install php-dom
apt-get install php-mbstringThe library provides methods to interact with SharePoint lists. Note that most operations require calling executeQuery() to submit the request to the server.
// Read list items
$list = $client->getWeb()->getLists()->getByTitle("{list-title}");
$items = $list->getItems();
$client->load($items);
$client->executeQuery();
// Create a list item
$itemProperties = array('Title' => 'New Item', 'Body' => 'Content');
$item = $list->addItem($itemProperties)->executeQuery();
// Update a list item
$listItem = $list->getItemById("{item-id}");
$listItem->setProperty('PercentComplete', 1);
$listItem->update()->executeQuery();
// Delete a list item
$listItem = $list->getItemById("{item-id}");
$listItem->deleteObject()->executeQuery();Execute the project's test suite using PHPUnit. You must use the provided bootstrap file and target the tests/sharepoint directory.
vendor/bin/phpunit --bootstrap tests/bootstrap.php --no-configuration tests/sharepointAccess OneDrive information using the Graph API through the GraphServiceClient.
use Office365\GraphServiceClient;
use Office365\Runtime\Auth\AADTokenProvider;
use Office365\Runtime\Auth\UserCredentials;
// ... acquireToken() implementation ...
$client = new GraphServiceClient("acquireToken");
$drive = $client->getMe()->getDrive()->get()->executeQuery();
print $drive->getWebUrl();Use the GraphServiceClient and Office365\Outlook classes to construct and send messages.
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;
// ... acquireToken() implementation as shown in Teams example ...
$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();To create a Team, you must first acquire an AAD token using an AADTokenProvider and then use the GraphServiceClient to call the Teams API.
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();Use an App Principal with a client certificate for enhanced security. You will need the tenant name, client ID, the private key content, and the certificate thumbprint.
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}");Authenticate using standard username and password credentials.
use Office365\Runtime\Auth\UserCredentials;
use Office365\SharePoint\ClientContext;
$credentials = new UserCredentials("{userName}", "{password}");
$ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);Use an App Principal with a Client ID and Client Secret to authenticate with SharePoint Online or On-Premises. This is suitable for service-to-service communication.
use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ClientContext;
$credentials = new ClientCredential("{clientId}", "{clientSecret}");
$ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);For SharePoint On-Premises environments, use NTLM authentication with user credentials.
use Office365\Runtime\Auth\UserCredentials;
use Office365\SharePoint\ClientContext;
$credentials = new UserCredentials("{userName}", "{password}");
$ctx = (new ClientContext("{siteUrl}"))->withNtlm($credentials);An Application object in the Microsoft 365 directory can act as different types of clients. You can retrieve the specific configuration object for the application type using the following methods:
| Method | Returns | Description |
|---|---|---|
getApi() | ApiApplication | Configuration for API-based applications. |
getPublicClient() | PublicClientApplication | Configuration for public client applications. |
getWeb() | WebApplication | Configuration for web-based applications. |
getInfo() | InformationalUrl | Information about the application's URL. |
getOptionalClaims() | OptionalClaims | Configuration for optional claims. |
getParentalControlSettings() | ParentalControlSettings | Settings for parental controls. |