You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

jiska78's avatar
jiska78
Explorer | Level 3
8 years ago

Code for APIv2

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

 

  • 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.

  • 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);
    
        ?>

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    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's avatar
      jiska78
      Explorer | Level 3

      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's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,882 PostsLatest Activity: 10 hours ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!