tarask/onedrive
Composer 安装命令:
composer require tarask/onedrive
包简介
Microsoft Graph Api - OneDrive for PHP
README 文档
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");
统计信息
- 总下载量: 1.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-08