onlinecode

php Download OneDrive Item – how to Download OneDrive Item using rest api

php Download OneDrive Item – how to Download OneDrive Item using rest api

in this post we will show you php Download OneDrive Item. For create onedrive folder you need register you app and get CLIENT-ID, CLIENT-SECRET and CALLBACK-URL.

For add CLIENT-ID, CLIENT-SECRET and CALLBACK-URL go to src/functions.inc.php. Pass CALLBACK-URL as example/callback.php

This is complete package for onedrive rest api using php ::

Download

php Download OneDrive Item suing rest api

Use this API we can download the contents for an item with the File facet.

Prerequisites Download a OneDrive Item contents using php

To call the OneDrive Item contents using php , the user must have granted/need the application read access to the file the app wishes to download.

HTTP request for php Download OneDrive Item using rest api

GET /drive/items/{your-item-id}/content
GET /drive/root:/{path and filename}:/content

Example for php Download OneDrive Item using rest api

GET /drive/items/{your-item-id}/content
<?php

// code for Download OneDrive Item
// go to downlode package and include header.inc.php 
require_once "header.inc.php";
// go to downlode package and in "src" folder and  include functions.inc.php 
require_once "../src/functions.inc.php";

// we Call this function to grab 
// a current false or access_token if none this is available.
$get_token = skydrive_tokenstore::acquire_token();

if (!$get_token) {
	echo "Error : token not found !!! Login or Try again.";
} else {
	$onedrive_obj = new skydrive($get_token);
	try {
		$response_data = $onedrive_obj->download($_GET['fileid']);
		ob_end_clean();  
		header('Content-Type: application/octet-stream');
		header('Content-Length: '.$response_data[0]['properties']['size']);
		header('Content-Description: File Transfer');
		header('Content-Disposition: attachment; filename='.$response_data[0]['properties']['name']);		
		$std_out = fopen('php://output', 'r+');
		fwrite($std_out, $response_data[0]['data']);
	} catch (Exception $exception) {
		// An error occured, 
		echo "Error for Download OneDrive Item :: ".$exception->getMessage();
		exit;
	}
}
require_once "footer.inc.php";

You may also like onedrive file upload using php

Exit mobile version