Skip to content

calebdevelop/onedrive-api-for-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

One Drive Api for PHP

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");

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages