适用于 PHP 的 Microsoft 365 库。
Microsoft 365 Library for PHP. A REST/OData based client library for Microsoft 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 installed, execute the following command in your project root to install this library:
composer require vgrem/php-spoor via composer.json file:
{
"require": {
"vgrem/php-spo": "^3"
}
}Finally, be sure to include the autoloader:
require_once '/path/to/your-project/vendor/autoload.php';PHP version: PHP 7.1 or later
The list of supported SharePoint versions:
The following auth flows supported:
use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ClientContext;
$credentials = new ClientCredential("{clientId}", "{clientSecret}");
$ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials);Documentation:
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:
use Office365\Runtime\Auth\UserCredentials;
use Office365\SharePoint\ClientContext;
$credentials = new UserCredentials("{userName}", "{password}");
$ctx = (new ClientContext("{siteUrl}"))->withCredentials($credentials); use Office365\Runtime\Auth\UserCredentials;
use Office365\SharePoint\ClientContext;
$credentials = new UserCredentials("{userName}", "{password}");
$ctx = (new ClientContext("{siteUrl}"))->withNtlm($credentials);
The following examples demonstrates how to perform basic CRUD operations against SharePoint list item resources:
Example 1. How to read SharePoint list items:
…or via fluent API syntax:
…Example 2. How to create SharePoint list item:
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:
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:
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();The following is an example of a minimal request to create a Team (via delegated permissions)
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();
Supported list of APIs:
The following example demonstrates how to send a message via Outlook Mail API:
…The following example demonstrates how retrieve My drive Url via OneDrive 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");
$drive = $client->getMe()->getDrive()->get()->executeQuery();
print $drive->getWebUrl();
暂无开放 Issues,或尚未同步最近议题。