cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Code for APIv2

Code for APIv2

jiska78
Explorer | Level 3
Go to solution

Sorry, I'm just a hack trying to upload a database backup to dropbox. API v1 worked fine but now I'm forced to migrate.

 

Here's the code I used to use, with the old API v1 commands and the new API v2 ones.

 

Not working - can someone please help?

 

use Dropbox\Dropbox;
use Dropbox\DropboxApp;
use Dropbox\DropboxFile;
$app = new DropboxApp(1,2,3);
$dropbox = new Dropbox($app);
$backupFile = "/home/edplorga/backups/temp/db_" . date('d-m-Y').".zip";
$backupFilename = "/db_" . date('d-m-Y').".zip";
 
//Old stuff
$appInfo = dbx\AppInfo::loadFromJsonFile(DIR."/config.json");
$dbxClient = new dbx\Client($accessToken, "EDPL_SQL_Backups");
$f = fopen($backupFile, "rb");
$result = $dbxClient->uploadFile($backupFilename, dbx\WriteMode::force(), $f);
fclose($f);
 
//New stuff
$dropboxFile = DropboxFile::createByStream($backupFilename, $backupFile);

 

2 Accepted Solutions

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

If you want to call the Dropbox API directly, rather than via a library, you can makes HTTPS connections to the endpoints themselves. For example, to upload a file, you would use /2/files/upload. Calling that using curl in PHP would look something like this:

 

    <?php

    $path = 'test_php_upload.txt';
    $fp = fopen($path, 'rb');
    $size = filesize($path);

    $cheaders = array('Authorization: Bearer <ACCESS_TOKEN>',
                      'Content-Type: application/octet-stream',
                      'Dropbox-API-Arg: {"path":"/test/'.$path.'", "mode":"add"}');

    $ch = curl_init('https://content.dropboxapi.com/2/files/upload');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, $size);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    echo $response;
    curl_close($ch);
    fclose($fp);

    ?>

`<ACCESS_TOKEN>` should be replaced with the OAuth 2 access token.

View solution in original post

Greg-DB
Dropbox Staff
Go to solution

The uploaded file path in Dropbox is defined by the "path" parameter in Dropbox-API-Arg.

 

So, for example, to upload a file "test_php_upload.txt" to root, the path parameter should be "/test_php_upload.txt".

 

Here's a version of my previous example that would do that:

 

    <?php

    $path = 'test_php_upload.txt';
    $fp = fopen($path, 'rb');
    $size = filesize($path);

    $cheaders = array('Authorization: Bearer <ACCESS_TOKEN>',
                      'Content-Type: application/octet-stream',
                      'Dropbox-API-Arg: {"path":"/'.$path.'", "mode":"add"}');

    $ch = curl_init('https://content.dropboxapi.com/2/files/upload');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, $size);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    echo $response;
    curl_close($ch);
    fclose($fp);

    ?>

 

View solution in original post

11 Replies 11

Greg-DB
Dropbox Staff
Go to solution

It looks like you're using the third party PHP SDK kunalvarma05/dropbox-php-sdk. That's made by a third party so I'm afraid I can't offer help with it.

 

I recommend referring to it's documentation for information on how to use it, or opening an issue there if you need help with it.

 

(If the Dropbox API itself isn't working as expected of course, please share the unexpected behavior/error and we'll be happy to help.)

jiska78
Explorer | Level 3
Go to solution

Hi Greg, thanks for your reply. I'm more than happy to use the Dropbox API rather than a 3rd Party. Not too fussed how it connects to be honest, as long as it does. How do I go about using the Dropbox API to connect and upload a file?

Greg-DB
Dropbox Staff
Go to solution

If you want to call the Dropbox API directly, rather than via a library, you can makes HTTPS connections to the endpoints themselves. For example, to upload a file, you would use /2/files/upload. Calling that using curl in PHP would look something like this:

 

    <?php

    $path = 'test_php_upload.txt';
    $fp = fopen($path, 'rb');
    $size = filesize($path);

    $cheaders = array('Authorization: Bearer <ACCESS_TOKEN>',
                      'Content-Type: application/octet-stream',
                      'Dropbox-API-Arg: {"path":"/test/'.$path.'", "mode":"add"}');

    $ch = curl_init('https://content.dropboxapi.com/2/files/upload');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, $size);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    echo $response;
    curl_close($ch);
    fclose($fp);

    ?>

`<ACCESS_TOKEN>` should be replaced with the OAuth 2 access token.

jiska78
Explorer | Level 3
Go to solution

Brilliant Greg, that's working, thankyou.
One problem tho, I am uploading from a local path and it seems to be holding that path in to Dropbox. I just want to upload to the root folder in Dropbox, but every change I make seems to break the code.
Are you able to assist please?

<?php

$filename = "/db_" . date('d-m-Y').".zip";
$path = "/dir1/dir2/dir3/dir4" . $filename;
$fp = fopen($path, 'rb');
$size = filesize($path);

$cheaders = array('Authorization: Bearer 1234567890',
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"'.$path.'", "mode":"add"}');

$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

echo $response;
curl_close($ch);
fclose($fp);

Greg-DB
Dropbox Staff
Go to solution

The uploaded file path in Dropbox is defined by the "path" parameter in Dropbox-API-Arg.

 

So, for example, to upload a file "test_php_upload.txt" to root, the path parameter should be "/test_php_upload.txt".

 

Here's a version of my previous example that would do that:

 

    <?php

    $path = 'test_php_upload.txt';
    $fp = fopen($path, 'rb');
    $size = filesize($path);

    $cheaders = array('Authorization: Bearer <ACCESS_TOKEN>',
                      'Content-Type: application/octet-stream',
                      'Dropbox-API-Arg: {"path":"/'.$path.'", "mode":"add"}');

    $ch = curl_init('https://content.dropboxapi.com/2/files/upload');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
    curl_setopt($ch, CURLOPT_PUT, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, $size);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    echo $response;
    curl_close($ch);
    fclose($fp);

    ?>

 

jiska78
Explorer | Level 3
Go to solution
Strange I thought I did that but brilliant - all working well now.

Thankyou very much for your help Greg, it is greatly appreciated.

mabramsphoto
New member | Level 2
Go to solution

Hi Greg,

I used the code that you provided and I was able to get it to work!  It's different than the Postman Collection so the fopen definitely helped.

 

Anyway, I am coming across the problem of the api not changing the filename if there is already the same named file inside the folder, adding the (#) at the end of the file name.  

I have "mode" : "add", "autorename" : true but still nada.  

 

When playing around with the PHP Curl code from Postman, it worked as soon as I removed the drive letter in the path, but no luck here.  Below is my code...any help is greatly appreciated...Thank you!

 

<?php

$path = '\Documents\ShXXSh\Sample Report.pdf';
$fp = fopen($path'rb');
$size = filesize($path);

$cheaders = array('Dropbox-API-Arg: { "path": "/Bxxx Ox Dxxxxxx/ShXXSh/Test7-9-21/Clue/Sample Report.pdf", "mode": "add", "autorename": true, "mute": false, "strict_conflict": false}',
'Dropbox-API-Path-Root: {".tag": "namespace_id", "namespace_id": "XXX"}',
'Dropbox-API-Select-User: dbmid:XXXXXXXXXXXXXXXXXXXXX',
'Content-Type: application/octet-stream',
'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

echo $response;
curl_close($ch);
fclose($fp);

?>

tahsini
Dropbox Staff
Go to solution

What is the current behavior you're seeing after uploading? Do you not see any new file uploaded, or do you see the file being updated?

Greg-DB
Dropbox Staff
Go to solution

@mabramsphoto Also, note that Dropbox won't consider it a conflict (that is, won't make a new file), if you upload the same contents as the existing file. You should upload different contents to see that behavior, or set "strict_conflict" to true if you want that behavior even for identical contents.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    yunu New member | Level 2
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    tahsini Dropbox Staff
  • User avatar
    mabramsphoto New member | Level 2
What do Dropbox user levels mean?