tarask / onedrive
Microsoft Graph Api - OneDrive for PHP
Installs: 1 708
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 2
Forks: 3
Open Issues: 4
Requires
- php: ^5.6 || ^7.0
- guzzlehttp/guzzle: ^6.3
- symfony/property-access: ^3.4
- symfony/property-info: ^3.4
- symfony/serializer: ^3.4
- symfony/yaml: ^3.4
This package is not auto-updated.
Last update: 2025-05-22 13:11:20 UTC
README
The OneDrive REST API Official Documentation
https://docs.microsoft.com/en-us/onedrive/developer/rest-api/
Installation
$ composer require tarask/onedrive
Generate token
Get an Authorization code :
require __DIR__.'/vendor/autoload.php';
use Tsk\OneDrive\Services\OneDriveService;
use Tsk\OneDrive\Client;
$client = new Client();
$client->setClientId('xxxxxxxx');
$client->setClientSecret('xxxxxxxx');
$client->setRedirectUri('http://localhost');
$client->setScopes([
OneDriveService::ONEDRIVE_OFFLINE_ACCESS,
OneDriveService::ONEDRIVE_FILE_READ,
OneDriveService::ONEDRIVE_FILE_READ_ALL,
OneDriveService::ONEDRIVE_FILE_READ_WRITE,
OneDriveService::ONEDRIVE_FILE_READ_WRITE_ALL
]);
$authUrl = $client->createAuthUrl();
echo $authUrl;
Go to the browser and enter the generated url.
After authentication, you will be redirected to http://localhost?code=xxxxxxx
Redeem the code for access tokens :
After getting the code
value, you can recover your access token with $client->fetchAccessTokenWithAuthCode($_GET['code'])
.
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
file_put_contents(__DIR__.'/token.json', \json_encode($token));
Upload large files
$token = file_get_contents(__DIR__.'/token.json');
$client->setAccessToken($token);
$file = '/path/to/the/file.docx';
$handle = fopen($file, 'rb');
$fileSize = filesize($file);
$chunkSize = 1024*1024;
$media = new MediaFileUpload($client, 'filename.docx', 'folderId', true, $chunkSize);
$media->setFileSize($fileSize);
$res = null;
while (!feof($handle)) {
$bytes = fread($handle, $chunkSize);
$res = $media->nextChunk($bytes);
}
echo 'FileId : ' . $res['id'];
print_r($res)
Create folder
$service = new OneDriveService($client);
$service->items->createFolder("folder_name");
On this sample, folder_name
is create in root folder.
You can also create a subdirectory by adding folderId as a second parameter.
$service->items->createFolder("folder_name", "parent_folder_id");