Use this link https://www.dropbox.com/developers/documentation/http/documentation for documentation
Create 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 |
https://www.dropbox.com/oauth2/authorize?client_id={client_id}&response_type=token&redirect_uri={redirect_uri}; // for this url you will get token |
Get current user data
1 2 3 4 5 6 7 8 9 |
//using the above link you will get token and account_id $userdata = CurlCall("https://api.dropboxapi.com/2/users/get_account","POST",array( // Set here requred headers "accept: */*", "accept-language: en-US,en;q=0.8", "content-type: application/json", "Authorization: Bearer ". $request->access_token ),json_encode(array('account_id'=>$request->account_id))); $userdataarray = json_decode($userdata); |
Create a upload file 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 |
// upload file in dropbox function Upload_File_Dropbox($token,$foloderStructure,$filename,$localfilepath){ $api_url = 'https://content.dropboxapi.com/2/files/upload'; //dropbox api url $headers = array('Authorization: Bearer '. $token, 'Content-Type: application/octet-stream', 'Dropbox-API-Arg: '. json_encode( array( "path"=> $foloderStructure. basename($filename), "mode" => "add", "autorename" => true, "mute" => false ) ) ); $ch = curl_init($api_url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $path = $localfilepath; $fp = fopen($path, 'rb'); $filesize = filesize($path); curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_setopt($ch, CURLOPT_VERBOSE, 1); // debug $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $err = curl_error($ch); curl_close($ch); if ($err) { return $err; } else { return $response; } } |
Upload file
1 2 |
$OneDriveUploaded = Upload_File_One_Drive($localfilepath,$foldername.$filename,$onedrivetoken); $onedriveuploadedarray = json_decode($OneDriveUploaded); // uploaded file response |
Create Folder in dropbox
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 |
// create folder in dropbox function Create_Dropbox_Folder($token,$path){ $fields = [ "path"=>$path ]; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.dropboxapi.com/2/files/create_folder_v2", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30000, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => json_encode($fields), CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_HTTPHEADER => array( // Set here requred headers "accept: */*", "accept-language: en-US,en;q=0.8", "content-type: application/json", "Authorization: Bearer ". $token ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { return $err; } else { return $response; } } |
Using an App Folder to store user content without access to all files
Go to Api console and create new app and set Permission type: App folder
Leave a reply