Dropbox’s API is a powerful feature of an already handy service. However, being primarily a PHP developer I was a little disappointed that they didn’t include an official SDK for this platform and what third-party libraries that were available unfortunately came with a number of bugs. As such we have decided to give a brief introduction to using the Dropbox API in a PHP development environment.
Hopefully by following this example it’ll help you skip the basic grunt work and get you into experimenting with the cool features that the Dropbox API provides.
First thing we need to do is log into Dropbox using the account you wish to associate your test app with. Head over to dropbox.com and login. We will need to register our test app with Dropbox. Registering an app will generate a unique ‘app key’ and ‘app secret’ which we will use to access the Dropbox API a bit later. To register, head to
https://www.dropbox.com/developers/apps and click the big blue button to create your app. Enter your new app’s details and for the purposes of this introduction select the ‘App folder’ option.
Once you’ve created the app you’ll be given your ‘app key’ and ‘app secret’ plus an opportunity to add more details about your app. Now that we’ve registered the app with Dropbox we can start to build a basic framework to interact with the Dropbox API.
Firstly, let’s create a configuration file that will hold our ‘app key’ and ‘app secret’. We will be using the config file throughout the process. In your development environment create a config.php file and add the following code (or update the example we have provided):
<?php $app_key = 'xxxxxxxxxxxxx'; $app_secret = 'xxxxxxxxxxxxx';
Replace the ‘xxxxxxxxx’s with your key/secret pair.
Our main goal will be to create an authentication process that will basically give a user permission to interact with our Dropbox app. The Dropbox API uses OAuth (oauth.net) to authenticate a user. Have a look at ‘step-1.php’ of our example. Make sure you edit the ‘step_2_url’ variable at the top of the script to your own URL destination.
To summarise, this script requests an OAuth token from Dropbox using cURL (a ‘token’ being a unique character string that will be used during the authentication process). Dropbox will send back a token plus a ‘secret’. For the purpose of this example we are storing the return token and its associated ‘secret’ in a session variable. Please note that this is neither the best or securest way of storing the data but as it’s a simple example it will do for this exercise.
The last part of this step redirects the user to the Dropbox website. From a user’s point-of-view, if all goes well they should not see any output within this script and simply be redirected to the Dropbox landing page. On this landing page they will be asked to allow your Dropbox app permission to connect to the user’s Dropbox account.
When they confirm the connection they will be taken to ‘step-2.php’ along with a set of response variables. Behind the scenes Dropbox will create a folder within the user’s account that can be interacted with via our app. At the top of ‘step-2.php’ make sure you change the ‘step_3_url’ variable to your own URL destination. In summary, ‘step-2.php’ will validate the response from when the user confirmed the connection to your app and it will then send a request for an ‘access’ token (as opposed to the ‘request’ token received in the first step).
Once the access token and it’s secret are received we will once again store these variables in our session variable. The end of this script will redirect us to the final step where the magic happens. Open up ‘step-3.php’ and have a look. Using our stored ‘access’ token we can now perform any of the Dropbox API calls available to us.
In our example we are requesting the metadata of the contents of the app folder created in the user’s account and then printing the data on the screen. This is specified via the following line:
curl_setopt( $ch, CURLOPT_URL, 'https://api.dropbox.com/1/metadata/sandbox' );
For a better example, log into the user’s account and upload a photo to the app’s folder located in the main Apps directory. Run ‘step-3.php’ again and you should now see the meta data for your image. As your token is saved in the session variable you should not need to run ‘step-1.php’ again. You will only need to start again if the session variable is lost or the token expires. Have a play around with the different API calls available and please feel free to build upon the example we’ve provided.
Please remember that the example scripts were built to provide a basic example of how the Dropbox API works in PHP. There are plenty of items that can be improved upon and we do not recommend using this code for production purposes. We hope this helps give you some guidance.