Use this link https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online for documentation
creating a function for curl call
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
function CurlCall($url,$method,$header,$fields){ $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30000, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_POSTFIELDS => $fields, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_HTTPHEADER => $header, )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { return $err; } else { return $response; } } |
Create Auth Url
1 2 |
// using for scope = User.Read files.read files.read.all files.readwrite files.readwrite.all offline_access https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={client_id}&scope={scope}&response_type=code&redirect_uri={redirect_uri} |
Create callback for get access token
1 2 3 4 5 6 7 8 9 |
// using auth url you will get code $fields = "client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}&code={code}&grant_type=authorization_code"; // get access token $tokendata = CurlCall("https://login.microsoftonline.com/common/oauth2/v2.0/token","POST",array( // Set here requred headers "content-type: application/x-www-form-urlencoded", ),$fields); // $tokendata is jsonencoded data which have token and refresh token data |
Get current user data
1 2 3 4 5 6 |
$token = json_decode($tokendata); $userdata = CurlCall("https://graph.microsoft.com/v1.0/me","GET",array( // Set here requred headers "content-type: application/json", "Authorization: Bearer ". $token->access_token ),""); |
Get Refresh Token
1 2 3 4 5 6 |
$token = json_decode($tokendata); $fields = "client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}&refresh_token=".$token ->refresh_token."&grant_type=refresh_token"; $newtokendata = CurlCall("https://login.microsoftonline.com/common/oauth2/v2.0/token","POST",array( // Set here requred headers "content-type: application/x-www-form-urlencoded", ),$fields); |
Upload File in One Drive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$token = json_decode($tokendata); function Upload_File_One_Drive($file_path,$folderwithfile,$token){ $image = fopen($file_path, "rb"); $curl = curl_init(); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); curl_setopt($curl, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me/drive/root:/".$folderwithfile.":/content"); curl_setopt($curl, CURLOPT_PUT, 1); curl_setopt($curl, CURLOPT_INFILE, $image); curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file_path)); curl_setopt($curl, CURLOPT_HTTPHEADER, array( // Set here requred headers "content-type: application/json", "Authorization: Bearer ". $token )); $result = curl_exec($curl); curl_close($curl); return $result; } // $file is your localfolder file path or url // $upload_folder_with_file is onedrive folder with file exp = myfolder/file.txt $OneDriveUploaded = Upload_File_One_Drive($file,$upload_folder_with_file,$token->access_token); |
Using function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
// $localfilepath is where you file store in localsystem Upload_File_One_Drive($localfilepath,$foldername.$filename,$onedrivetoken); // upload file in one drive function Upload_File_One_Drive($file_path,$folderwithfile,$token){ $file = file_get_contents($file_path); $fileSize = strlen($file); if($fileSize > 4194304){ $server_output = false; $url='https://graph.microsoft.com/v1.0/me/drive/root:/'.$folderwithfile.':/createUploadSession'; $ch = curl_init(); $data= '{}'; $header = array("content-type: application/json", "Cache-Control: no-cache", "Pragma: no-cache", "Authorization: Bearer ". $token.""); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $jsonresult = curl_exec($ch); $result = json_decode($jsonresult); $graph_url = $result->uploadUrl; $fragSize = 320 * 1024; $numFragments = ceil($fileSize / $fragSize); $bytesRemaining = $fileSize; $i = 0; $ch = curl_init($graph_url); while ($i < $numFragments) { $chunkSize = $numBytes = $fragSize; $start = $i * $fragSize; $end = $i * $fragSize + $chunkSize - 1; $offset = $i * $fragSize; if ($bytesRemaining < $chunkSize) { $chunkSize = $numBytes = $bytesRemaining; $end = $fileSize - 1; } if ($stream = fopen($file_path, 'r')) { // get contents using offset $data = stream_get_contents($stream, $chunkSize, $offset); fclose($stream); } $content_range = " bytes " . $start . "-" . $end . "/" . $fileSize; $headers = array( "Content-Length: $numBytes", "Content-Range:$content_range" ); curl_setopt($ch, CURLOPT_URL, $graph_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $server_output = curl_exec($ch); $info = curl_getinfo($ch); $bytesRemaining = $bytesRemaining - $chunkSize; $i++; } return $server_output; }else{ $image = fopen($file_path, "rb"); $curl = curl_init(); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); curl_setopt($curl, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me/drive/root:/".$folderwithfile.":/content"); curl_setopt($curl, CURLOPT_PUT, 1); curl_setopt($curl, CURLOPT_INFILE, $image); curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file_path)); curl_setopt($curl, CURLOPT_HTTPHEADER, array( // Set here requred headers "content-type: application/json", "Authorization: Bearer ". $token )); $result = curl_exec($curl); curl_close($curl); fclose($image); return $result; } } |
Using an App Folder to store user content without access to all files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
function Upload_File_One_Drive($file_path,$folderwithfile,$token){ $file = file_get_contents($file_path); $fileSize = strlen($file); if($fileSize > 4194304){ $server_output = false; $url='https://graph.microsoft.com/v1.0/me/special/approot:/'.$folderwithfile.':/createUploadSession'; $ch = curl_init(); $data= '{}'; $header = array("content-type: application/json", "Cache-Control: no-cache", "Pragma: no-cache", "Authorization: Bearer ". $token.""); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $jsonresult = curl_exec($ch); $result = json_decode($jsonresult); $graph_url = $result->uploadUrl; $fragSize = 320 * 1024; $numFragments = ceil($fileSize / $fragSize); $bytesRemaining = $fileSize; $i = 0; $ch = curl_init($graph_url); while ($i < $numFragments) { $chunkSize = $numBytes = $fragSize; $start = $i * $fragSize; $end = $i * $fragSize + $chunkSize - 1; $offset = $i * $fragSize; if ($bytesRemaining < $chunkSize) { $chunkSize = $numBytes = $bytesRemaining; $end = $fileSize - 1; } if ($stream = fopen($file_path, 'r')) { // get contents using offset $data = stream_get_contents($stream, $chunkSize, $offset); fclose($stream); } $content_range = " bytes " . $start . "-" . $end . "/" . $fileSize; $headers = array( "Content-Length: $numBytes", "Content-Range:$content_range" ); curl_setopt($ch, CURLOPT_URL, $graph_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $server_output = curl_exec($ch); $info = curl_getinfo($ch); $bytesRemaining = $bytesRemaining - $chunkSize; $i++; } return $server_output; }else{ $image = fopen($file_path, "rb"); $curl = curl_init(); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); curl_setopt($curl, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me/drive/special/approot:/".$folderwithfile.":/content"); curl_setopt($curl, CURLOPT_PUT, 1); curl_setopt($curl, CURLOPT_INFILE, $image); curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file_path)); curl_setopt($curl, CURLOPT_HTTPHEADER, array( // Set here requred headers "content-type: application/json", "Authorization: Bearer ". $token )); $result = curl_exec($curl); curl_close($curl); fclose($image); return $result; } } |
Leave a reply