https://docs.microsoft.com/en-us/onedrive/developer/rest-api/
$ composer require tarask/onedrive
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
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));
$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)
$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");