DropPHP – einfacher PHP Dropbox API Client ohne cURL
DropPHP ist eine kompakte PHP Klasse, die eine einfache Schnittstelle zur Dropbox REST API zur Verfügung stellt. Sie bietet Funktionen zum Auflisten, Hoch- und Runterladen von Dateien an.
DropPHP verwendet für HTTPS-Requests die in PHP eingebauten Stream-Wrapper, daher werden keine besonderen PHP Bilbiotheken wie PECL oder cURL benötigt.
Es steht unter einer BSD-Lizenz zur Verfügung, hier die GitHub Seite.
Funktionen
Die Klasse DropboxClient enthält folgende Funktionen.
Bei einem Fehler wird eine DropboxException ausgeworfen, achte daher darauf, dass du die Funktionen in einem try-catch-Block aufrufst!
Funktionen dropbox_file/dropbox_path als Parameter haben, akzeptieren diesen als string oder Metadata-Objekt der Datei oder des Ordners.
| GetAccountInfo() | Ermittelt Informationen über den Benutzeraccount und gibt sie als Objekt zurück. |
|---|---|
| GetMetadata($dropbox_path) | Ermittelt Metadaten einer Datei oder eines Ordners. |
| GetFiles($dropbox_path=”, $recursive=false) | Listet Dateien in einem Verzeichnis auf. Ist $recursive = true, werden Unterverzeichnisse rekursiv gelistet. Da jedes Verzeichnis eine neue Anfrage erfordert, kann das einige Zeit daueren! |
| DownloadFile($dropbox_file, $dest_path=”, $revision=-1) | Lädt eine Datei aus der Dropbox auf den Webserver. $dest_path gibt an, wo die Datei gespeichert wird (Zeilverzeichnis muss existieren). Mit $revision kann die Revision der Datei angegeben werden. |
| UploadFile($src_file, $dropbox_path=”) | Lädt eine Datei vom Webserver in die Dropbox. Beachte, dass nur Dateien bis zu einer Größe von 150MB hochgeladen werden können! |
| GetLink($dropbox_file, $preview=true) | Gibt einen Link zu einer Dropbox-Datei zurück, mit dem jeder die Datei herunterladen kann. $preview gibt an, ob der Link auf eine Vorschauseite geht, anstatt direkt auf die Datei. |
| 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. |
Weitere Funktionen befinden sich in Entwicklung!
Authentifizierung
Die Dropbox API verwendet OAuth zur authentifizierung. Beim ersten Zugriff auf die Dropbox sendet der Dropbox server ein Request-Token. Der Benutzer wird zu einer Dropbox-Seite umgeleitet, wo er den Zugriff bestätigt. Anschließend wird mit dem gespeicherten Request-Token ein Access-Token ermittelt und gespeichert. Mit diesem lassen sich dann alle weiteren API-Anfragen authentifizieren.
DropPHP - einfacher PHP Dropbox API Client ohne cURL,
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?
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.
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
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
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
Upload file method doesn’t work.
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.
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.
Hi there,
Im unable to download a previous version of a file.
Is this happening to someone?
Regards.
Just add the parameters to the url and it’s working fine.
Regards.
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
The same problem. Help pls
same here
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
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.
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
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
@ 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?
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!
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
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.
Howdy! I simply would like to give an enormous thumbs up for
the good information you will have right here on this post.
I will be coming back to your weblog for more soon.