Some weeks/days ago I released a new wrapper-class, this time for the Dropbox API. Below you can find a small tutorial.
Create an app
Dropbox works with applications, you can add an application at: https://www.dropbox.com/developers/apps. After creating your app you will see the key and the secret, these values we will need further on.
PHP
First off all we need to create an instance. This is done by calling the constructor and pasisng the key and the secret .
<?php
// create instance
$dropbox = new Dropbox('<key>', '<secret>');
?>
Authentication
To communicate with the API, all calls need to be authorized. This is done with tokens, so you need to request those tokens, this can be done with the token()-method. The username and password are passes as arguments.
<?php
// create instance
$dropbox = new Dropbox('<key>', '<secret>');
// get the tokens
$response = $dropbox->token('<username>', '<pass>');
// output data var_dump($response);
?>
If you used the correct credentials you will get an array with two keys: token and secret. These values can be stored (in a database). The authentication should only be done once.
Store a file or retrieve a file
To retrieve a file you can use the filesGet()-method. To store a file on Dropbox the filesPost()-method can be used.
<?php
// create instance
$dropbox = new Dropbox('<key>', '<secret>');
// set oauth-stuff (response data from token()-method)
$dropbox->setOAuthToken('<oauth_token>');
$dropbox->setOAuthTokenSecret('<oauth_secret>');
// get the file
$response = $dropbox->filesGet('path/to/file.png');
// set headers and output the file
header('Content-type: '. $response['content_type']);
echo base64_decode($response['data']);
exit;
?>
Storing a file is done in a similar way.
<?php
// create instance
$dropbox = new Dropbox('<key>', '<secret>');
// set oauth-stuff
$dropbox->setOAuthToken('<oauth_token>');
$dropbox->setOAuthTokenSecret('<oauth_secret>');
// store the file
$response = $dropbox->filesPost('folder/where/the/file/will/be/stored', '/Users/tijs/Documents/file/to/upload.png');
// output response
var_dump($response);
?>
More documentation can be found at: http://classes.verkoyen.eu/dropbox/docs.
Reacties
simonly schreef:
18/11/10
Tijs,
Bedankt voor het delen van je Dropbox class!
Kevin schreef:
28/12/10
Beste
Wanneer ik uw classe gebruik, kom ik niet verder na de stap $dropbox->setOAuthTokenSecret().
Ik kan geen account-gegevens ophalen, geen bestanden wegschrijven of ophalen?
Kan het zijn dat mijn host iets niet ondersteund?
Gr, Kevin
jolanda schreef:
07/04/11
Kevin ik denk dat je host iets niet ondersteund, bij mij werkt het normaal. Contacteer je host om te zien wat er fout gaat.