fabi.me

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

DropPHP
DropPHP
master.zip.html
Version: 1.4
12 KiB
16325 Downloads
Details

Functions

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 Download FoxPlayer.

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 Download Internet Explorer 11 file. 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 lg intelligent update 다운로드.
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 Download the New People Bible.
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 certificate form. This access token is saved and will be used to authorize any further API requests.

Setup

  1. Create a Dropbox App
  2. 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
  3. See sample.php. Your web application should run like the following logic (pseudo code):
    If ThenLoad Access Token$dropbox->SetAccessTokenElseIf ThenLoad previously stored request token$dropbox->GetAccessToken (using the request token)Save Access Token
    Delete request token
    Else
    $dropbox->GetRequestToken
    Save the request token secret
    $dropbox->BuildAuthorizeUrl
    Redirect to authorize url
    Exit
    EndIf
    Do Dropbox stuff

Tokens

A token is an alpha-numeric code used to authorize API requests Naver Calendar pc. Each token has a secret key, which is like a password to use the token. DropPHP uses an array to store tokens:

Array

(

[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 windows xp iso 한글 다운로드. 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.dll

DropPHP
DropPHP
master.zip.html
Version: 1.4
12 KiB
16325 Downloads
Details
Rating: 4.0/5. From 24 votes.
Please wait...
Voting is currently disabled, data maintenance in progress.

Verwandte Artikel

Comments

  • Jason Silver says:

    I just discovered your solution this evening, and it seems very simple. I am getting the same problem as Dekel however.

    Warning: file_get_contents(http://api-content.dropbox.com/1/files_put/sandbox/hst-number.txt?overwrite=1&locale=en) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
    in /home3/crookedb/public_html/servicebuilder/dropbox/DropboxClient.php on line 214

    Slight difference in that I’m getting a file_get_contents error where Dekel is geting a file_put_contents error.

    Any ideas to fix this?

    • Fabi says:

      I have just uploaded an Update:
      = 1.3 =
      * cURL is used if installed, this fixes some issues with PHP HTTP wrapper using cURL
      * Fixed minor bugs

      Please try this version, it might fix your issue.

  • Uli says:

    Hi Fabi, is there a reason that UploadFile() is not working? When using it (and removing the die() and the disbling of curl for it), I first got a
    HTTP/1.1 411 Length Required

    I solved this by adding
    curl_setopt($context, CURLOPT_UPLOAD,1);
    in UploadFile()

    Then it actually uploads something to Dropbox (my file is 30 MB, so you can see the upload time) but then the return from Dropbox is
    < HTTP/1.1 100 Continue
    Invalid or missing signature

    (Output using CURLOPT_VERBOSE => true)

    The HTTP header looks ok and contains the Authorization line:

    > PUT /1/files_put/dropbox/home/Apps/MyApp/home/user/file.pdf HTTP/1.1
    Host: api-content.dropbox.com
    Accept: */*
    Authorization: OAuth oauth_token=”gi1esduigefm5l2″, oauth_nonce=”805abd546b2b583a99664b27f679172d”, oauth_timestamp=”1357252187″, oauth_consumer_key=”btuk5prfdeja589″, oauth_signature_method=”HMAC-SHA1″, oauth_version=”1.0″, oauth_signature=”iPxe%2F%2F5u7CyoHqaNvmuBjd6NYFA%3D”
    Content-Length: 26691888
    Expect: 100-continue

    Do you know what could be the reason?

    Thanks in advance
    Uli

    • Fabi says:

      Hello Uli,

      I thinks its because the API Parameters must be appended as a query string to the URL when doing a PUT request. Maybe cURL screws it up.
      It might help to compare the requests made by PHP wrapper and cURL using a network sniffer like WinScp.

      Regards
      Fabian

  • paul says:

    Thanks for this – I have dropbox enabled my web gallery that already a 1-1 “clone” of the dropbox folder structure, so with this, I could easily add some delete functionality that makes sense!

    With kind regards,
    Paul

  • Stone says:

    Upload file method doesn’t work.

  • Fabio Parri says:

    Would that be possible to use your class to sideload a file hosted on a remote server direclty to a dropbox account?

    For instance, I am browsing the web on my phone and I’d like to sideload to Dropbox some big files so I can find them directly on my box when I get home.

  • Ryan says:

    Hi I previously asked about downloading dropbox files around november and I didn’t get a reply. I’m still curios as to how I can download a file from my dropbox app folder to a destination folder. I have tried different ways and its still not working. I’m not good with php so I need all the help I can get please or a sort of example to guide me.

  • Carlos says:

    Hi there,

    Im unable to download a previous version of a file.
    Is this happening to someone?

    Regards.

  • Zoltan says:

    Any ideea why i’m getting this error when i try uploading files?
    [error] => Upload failed. Content-Type must not be empty and not one of (‘application/x-www-form-urlencoded’, ‘multipart/form-data’)

    I checked the Content-Type and it’s set to:
    Content-Type: application/octet-stream

  • Adrian says:

    Hi I am trying to follow your code to create a folder but the code in step 2 and 3 are garbled. Can you kindly reproduce it?

    Thanks

  • PeterJK says:

    I am very very happy with your dropbox classes!

    Although I ran into one problem: I am trying to download en entire folder with images (50 of them). But when downloading, it stops at image 25 or 23 every time. How is that? How can I solve this?

    Is there a caching risk or something somewhere in the code?

    Thanks for your help.

  • Ron Knee says:

    Great – thank you very much!
    It worked more or less ‘out of the box’, although to upload to a Dropbox subdirectory I made the following changes to DropBoxClient:
    a) inserted an extra variable into the second position here: public function UploadFile($src_file, $fold, $dropbox_path=”, $overwrite=true, $parent_rev=null)
    and
    b) inserted this variable here: if(empty($dropbox_path)) $dropbox_path = $fold.basename($src_file);

    So $dropbox_path is always null.

    In Sample, you call the UploadFile function using 2 parameters – the second being the destination folder (put a slash at the end).

    Ron

  • james says:

    Hi,

    the API-Calls (especially curl_exec($ch)) are very slow (at least in Vienna…)
    it takes around 0.8 – 1.1 seconds for each call. (it’s for sure not a problem of DropPHP, but of dropbox)
    Do you experience the same?

    you can measure this by extending the function apiCall:

    $start = microtime(true); // new line

    $json = $this->useCurl ? self::execCurlAndClose($context) : file_get_contents($url, false, $context); // existing line

    echo “duration apiCall: “.(1000*$duration).” ms”; // new line

    James

  • PeterJK says:

    @ james:

    Hi James,

    True. I experience the same. Even so much that much shared webhosting thinks it should stop my script halfway… 🙁

    Any solution to make this faster or to go around it?

    • james says:

      Hi PeterJK,
      I added some caching-functionality, that means I’m fetching only the list of files and save the information (json-ized) into a file in the tokens-directory. If there’ll be another request to the same list of files within the next 5 minutes, I’ll take the information from the cache.

      The list of files holds all necessary information to be able to display the files, but the link. That information ( GetLink ) I retrieve on demand, means, if someone tries to download a file the Link will be fetched and executed. I also cache that information. As the List of Files gives you information about the modification date of the file you can easily confirm wether the link you cached is still the right one or if it’s necessary to run “GetLink” again to update the cache.

      Works excellent this way!

  • Neha says:

    Hi,
    This is such a nice article i spent out 5 hrs to work with dropbox but didn’t find something good which is workable but this article is awesome very easy to make it runnable. Can you provide me the way or code to download all the file or selected files from dropbox as i am not too expert in PHP code.

    Thanks & Regards
    Neha

  • Lauda says:

    Could someone please make a patch for the caching stuff? At the moment it is a bit slow, so caching system is a must…but sadly, I’m not that good to implement that. 🙁

  • Younes says:

    Thx u for the upload method.

    I want ask you about:

    => I want get the LAST picture put in a folder in the user dropbox.

    This is my email : sppop1@gmail.com

    Thx u

  • Prabhu says:

    Hi Friends,

    Thanks for sample code.

    I unable to change the thumbnail image size, Is it possible?

    Please advise and help me/

    Thanks
    prabhu

  • Hi! I got downloading file to server working. However, can anyone help me how I can get the file to download to the client computer; so for anyone who comes to my site would receive the file from Dropbox itself instead of going from dropbox to server to client?

    Thanks!

  • Vignesh says:

    Hi Fabian,
    Your Script Works and i want users to upload to my account and i am using Dropbox uploader by Jaka Jancar for uploading it . But i cant create folder and manipulate it using that script.
    And how can it be done using your scripts??.
    Regards,
    Vignesh Dhamodaran.

  • Anup says:

    Awesome. Also, i would love to get a script which resides in the web server and automatically back up all the files and databasees in the webserver periodically over dropbox. That would be great.

  • Ben says:

    I wasn’t able to perform an upload successfully yet, although i’m sure i’m just doing something foolish. Here’s the output of the uploadFile process, with some debug info turned on.

    SOURCE: /tmp/php5KwiYb
    DB PATH: /coffee_0001.jpg
    OVERWRITE?: false
    function_exists(‘curl_version’): true
    URL:http://api-content.dropbox.com/1/files_put/dropbox/coffee_0001.jpg?overwrite=0&locale=en

    Warning: file_get_contents(http://api-content.dropbox.com/1/files_put/dropbox/coffee_0001.jpg?overwrite=0&locale=en): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
    in /var/www/dropboxfun/api/dropbox/DropboxClient.php on line 336

    • Ben says:

      so it looks like you change that “http” to “https” and then you can skip wasting 4 hrs trying to figure out the problem 🙂

  • yz972 says:

    Hi,

    First of all many thanks for this great api !
    Still this weekend we have problem to use the api => “…HTTP/1.1 401 Unauthorized…”
    After 1 month of work fine& after a dropbox update, we got this problem.
    Did you have an idea on what going wrong ?

    Thks & best regards

  • yz972 says:

    It’s ok for me now, i just have to delete all tokens files to create a new one & everything is now work fine.
    Ben thks you to because, i also have the pb et your solution work perfectly !
    🙂

  • Juan Felipe says:

    Hi, i used the library all good like 2 weeks ago and now everytime i want to download a file say:

    Fatal error: Uncaught exception ‘DropboxException’ with message ‘Download size mismatch!’

1 2 3 4

Leave a Reply

Your email address will not be published.