DropPHP – simple PHP Dropbox API Client without cURL
DropPHP provides a simple interface for Dropbox’s REST API to list, download and upload files.
For authentication it uses OAuthSimple, HTTPS requests are made with PHP’s built in stream wrapper. It does not require any special PHP libarys like PECL, cURL or OAUTH.
It is released under a BSD license and there is a GitHub page.
|Date:||9. December 2012|
The class DropboxClient provides the following API functions.
On failure a DropboxException is thrown, so make sure you call the functions in a try-catch block.
Functions having argument dropbox_file/dropbox_path accept this to be a string or file/folder metadata.
|GetAccountInfo()||Retrieves information about the user’s account. Returns an object with account data.|
|GetMetadata($dropbox_path)||Retrieves file and folder metadata.|
|GetFiles($dropbox_path=”, $recursive=false)||Get file list of a folder. Returns an array where keys the file paths and values metadata. If $recursive is true, all sub-folders are recursivly scanned. Note that this can take some time because each sub-folder causes a new API request.|
|DownloadFile($dropbox_file, $dest_path=”, $revision=-1)||
Downloads a file to the specified location on the webserver. The destionation directory has to exist.
$revision: The revision of the file to download. This defaults to the most recent revision. Returns metadata of the downloaded file.
|UploadFile($src_file, $dropbox_path=”)||Uploads a file from webserver to dropbox. Returns metadata of the uploaded file.|
|GetLink($dropbox_file, $preview=true)||Returns a download link to a file. If $preview is true it creates a link users can use to view a preview of the file in a web browser.|
|Delta($cursor)||Retrieves file changes. See delta|
|Copy($from_path, $to_path)||Copies a file or folder to a new location.|
|CreateFolder($path)||Creates a folder.|
|Delete($path)||Deletes a file or folder.|
|Move($from_path, $to_path)||Moves a file or folder to a new location.|
Dropbox REST API Authentication
Dropbox Rest API uses OAuth for authentication. When the Dropbox is accessed for the first time, the PHP Client retrieves a request token from Dropbox webservice. This token needs to be authorized for a Dropbox user account, so the user is redirected to an authentication page where they allow the app access to their account. Afterwards the user is redirected back to the web application, where DropPHP uses the previously stored request token (and secret) to retrieve an access token. This access token is saved and will be used to authorize any further API requests.
- Create a Dropbox App
- Copy & Paste the App key and secret to the PHP code, that creates the DropboxClient object:
$dropbox = new DropboxClient(array('app_key' => "YOUR_APP_KEY",‘app_secret’ => “YOUR_APP_SECRET”,’app_full_access’ => false, // if the app has full or folder only access),’en’); // language/country code
- See sample.php. Your web application should run like the following logic (pseudo code):
Load Access Token$dropbox->SetAccessTokenElseIf ThenLoad previously stored request token$dropbox->GetAccessToken (using the request token)Save Access Token
Delete request token
Save the request token secret
Redirect to authorize url
Do Dropbox stuff
A token is an alpha-numeric code used to authorize API requests. Each token has a secret key, which is like a password to use the token. DropPHP uses an array to store tokens:
[t] => 6lj3i20m4fsgi6y
[s] => 2pn7x2bjsm3xvq8
At Index t is the actual token, s is the token secret. In sample.php, this array is just serialized and written to a file (function store_token) in the folder /tokens. You should use your own method to save and load tokens (i.e. MySQL). Of course you can put the tokens in PHP’s $_SESSION array, then you have have to re-authenticate every new session.
If you get a wrapper not found error, make sure the PHP extension OpenSLL is loaded in php.ini:
extension=php_openssl.dllDropPHP – simple PHP Dropbox API Client without cURL,