Coder Social home page Coder Social logo

onedrive-api-for-php's Introduction

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

onedrive-api-for-php's People

Contributors

calebdevelop avatar

Stargazers

 avatar John Hoopes avatar  avatar  avatar Peter Olumolu avatar  avatar pagepan avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

onedrive-api-for-php's Issues

Complete set of example

Hi Caleb,

Thanks for your library, I believe it should be the simplest way to upload file to onedrive. But I found it difficult to come out a workable solution base on your code. Would you mind give us a complete set of sample code, including authentication process, to upload a file to OneDrive? Thanks so much.

Joanne

Conect one acount for multiple users

So I have an app that needs to connect to a specific OneDrive account to get just some files for any user at my web site.
But the tutorial sends the user to make a login at Microsoft so the OneDrive account that is been used is the one belonging to the user and not the one at my company.

How can I use only the account I need and also skips the authentication over Microsoft site?

Thank you all

Declared fragment length does not match the provided number of bytes

Client error: PUT https://api.onedrive.com/rup/8653f7cfbe8f5ceb/eyJSZXNvdXJjZUlEIjoiODY1M0Y3Q0ZCRThGNUNFQiE4NjYzNyIsIlJlbGF0aW9uc2hpcE5hbWUiOiJTYXBpZW5zLnBkZiJ9/4mlt4ccEqxSxs4awblzJEnFnxWB-G-5pTwG9Q3926CQhDc3CddijtJubdKnpL6-_ACg4sovO_NjIZGekiyUrl29XaFucEFr8IsexoxEXf7w14/eyJuYW1lIjoiU2FwaWVucy5wZGYiLCJkZXNjcmlwdGlvbiI6IiIsIkBuYW1lLmNvbmZsaWN0QmVoYXZpb3IiOiJyZW5hbWUifQ/4wKq8s1f9huNnMNv-fryQSFJxwzost0l2j4ge-icMyileL2k-1mVigWlmFRfclPZw4eXqqXY8sF4PaAOj34TlQ42Yr6B6YWP_0_b6YvVJj3yOKr8YifTwvBlXVccrvMsGnxI9zmZk-P3-Bq_-QV7rd0oRjFjWbXUglyw8C8QU2ed-clVb4zfbyS79lOoykyj4nPy0_CobqxfvphRRSuFSJwda1hr0RxzkxtekXhGNia8_djp1-6UUEL2nW1PnjBpeHmb98etgr2Rs_QA_ey0gfz6WKds28Y5ul-dLGFDJ5i5-ZeHIhmiVCJbV67_0qUNQYDvYcZyOyotz-lsjvs0l7l2bzYYBC82mvaLTevrfzPqCD8V02IOGk6uYr5lpyQU6iWhrDzhtNvSn-AU0nmrzb4nDbS3RLc0aVef-2C6QyqS6btZXO1AgMINUYD4odmv29qcpcXJ_e_sTHxjbC7rxojykCO5wOVGdjQjaosIARKcOC37ty9ag23ZzD2FsQrD6zK7QKq0P4rz-VJBNIEVjrrfv-rg85GK7KSHR4RUg3P5jEJOmK6zMZMKej3tg5vprOKPnc2KSURihmucs-LY3pl9SAoF6APoWhgjioaw3-snYbCQAepGR0HKfmHqZZnISj resulted in a 400 Bad Request response: {"error":{"code":"invalidRequest","message":"Declared fragment length does not match the provided number of bytes"}}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.