{"id":614,"date":"2012-12-10T00:35:26","date_gmt":"2012-12-09T22:35:26","guid":{"rendered":"http:\/\/fabi.me\/?p=614"},"modified":"2013-05-28T15:22:48","modified_gmt":"2013-05-28T13:22:48","slug":"dropphp-dropbox-api-client","status":"publish","type":"post","link":"https:\/\/fabi.me\/en\/php-projects\/dropphp-dropbox-api-client\/","title":{"rendered":"DropPHP \u2013 simple PHP Dropbox API Client without cURL"},"content":{"rendered":"<p>DropPHP\u00a0provides a simple interface for Dropbox\u2019s REST API to list, download and upload files.<\/p>\n<p>For authentication it uses\u00a0<a href=\"https:\/\/github.com\/jrconlin\/oauthsimple\" target=\"_blank\">OAuthSimple<\/a>,\u00a0HTTPS requests are made with PHP\u2019s built in stream wrapper. It does not require any special PHP libarys like PECL, cURL or OAUTH.<\/p>\n<p>It is released under a BSD license and there is a\u00a0<a href=\"https:\/\/github.com\/f4bsch\/DropPHP\">GitHub page<\/a> <a class=\"bafe\" href=\"http:\/\/backroadsandbeyondradio.com\/%ea%b0%93%ed%94%bc%ed%94%8c%ec%84%b1%ea%b2%bd-%eb%8b%a4%ec%9a%b4%eb%a1%9c%eb%93%9c\/\">Download the New People Bible<\/a>.<br \/>\n<iframe loading=\"lazy\" src=\"http:\/\/ghbtns.com\/github-btn.html?user=f4bsch&#038;repo=DropPHP&#038;type=watch&#038;count=true&#038;size=large\" allowtransparency=\"true\" frameborder=\"0\" scrolling=\"0\" width=\"170\" height=\"30\" style=\"float:right;\"><\/iframe><\/p>\n<div style=\"clear:both;\"><\/div>\n<div class=\"wpfilebase-file-default\" onclick=\"if('undefined' == typeof event.target.href) document.getElementById('wpfb-file-link-2').click();\">\r\n  <div class=\"icon\"><a href=\"https:\/\/fabi.me\/en\/download\/scripts\/master.zip.html\" target=\"_blank\" title=\"Download DropPHP\"><img align=\"middle\" src=\"https:\/\/fabi.me\/wp-includes\/images\/crystal\/code.png\" alt=\"DropPHP\" \/><\/a><\/div>\r\n  <div class=\"filetitle\">\r\n    <a href=\"https:\/\/fabi.me\/en\/download\/scripts\/master.zip.html\" title=\"Download DropPHP\" target=\"_blank\" id=\"wpfb-file-link-2\">DropPHP<\/a>\r\n    \r\n    <br \/>\r\n    master.zip.html<br \/>\r\n    Version: 1.4<br \/>\r\n  <\/div>\r\n  <div class=\"info\">\r\n    12 KiB<br \/>\r\n    17366 Downloads<br \/>\r\n    <a href=\"#\" onclick=\"return wpfilebase_filedetails(2);\">Details<\/a>\r\n  <\/div>\r\n  <div class=\"details\" id=\"wpfilebase-filedetails2\" style=\"display: none;\">\r\n  \r\n  <table border=\"0\">\r\n   \r\n   <tr><td><strong>Author:<\/strong><\/td><td>Fabian Schlieper<\/td><\/tr>\r\n   \r\n   \r\n   <tr><td><strong>Category:<\/strong><\/td><td>Scripts&Codes<\/td><\/tr>\r\n   \r\n   <tr><td><strong>Date:<\/strong><\/td><td>9. December 2012<\/td><\/tr>\r\n  <\/table>\r\n  <\/div>\r\n <div style=\"clear: both;\"><\/div>\r\n<\/div><div style=\"clear:both;\"><\/div>\n<h2>Functions<\/h2>\n<p>The class DropboxClient provides the following API functions.<\/p>\n<p>On failure a DropboxException is thrown, so make sure you call the functions in a try-catch block.<\/p>\n<p>Functions having argument dropbox_file\/dropbox_path accept this to be a string or file\/folder metadata.<\/p>\n<table>\n<tbody>\n<tr>\n<th>GetAccountInfo()<\/th>\n<td>Retrieves information about the user\u2019s account. Returns an object with\u00a0<a href=\"https:\/\/www.dropbox.com\/developers\/reference\/api#account-info.\">account data<\/a> <a class=\"bafe\" href=\"http:\/\/www.clippe.it\/%ec%82%b0%ed%83%80%eb%82%98-%eb%8b%a4%ec%9a%b4%eb%a1%9c%eb%93%9c\/\">\uc0b0\ud0c0\ub098<\/a>.<\/td>\n<\/tr>\n<tr>\n<th>GetMetadata($dropbox_path)<\/th>\n<td>Retrieves file and folder\u00a0<a href=\"https:\/\/www.dropbox.com\/developers\/reference\/api#metadata\">metadata<\/a>.<\/td>\n<\/tr>\n<tr>\n<th>GetFiles($dropbox_path=\u201d, $recursive=false)<\/th>\n<td>Get file list of a folder. Returns an array where keys the file paths and values\u00a0<a href=\"https:\/\/www.dropbox.com\/developers\/reference\/api#metadata\">metadata<\/a>. 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.<\/td>\n<\/tr>\n<tr>\n<th>DownloadFile($dropbox_file, $dest_path=\u201d, $revision=-1)<\/th>\n<td>Downloads a file to the specified location on the webserver <a class=\"bafe\" href=\"http:\/\/www.kloppskitchen.com\/%ec%88%98%ea%b0%95%ec%a6%9d%eb%aa%85%ec%84%9c-%ec%96%91%ec%8b%9d-%eb%8b%a4%ec%9a%b4%eb%a1%9c%eb%93%9c\/\">certificate form<\/a>. The destionation directory has to exist.<\/p>\n<p>$revision: The revision of the file to download. This defaults to the most recent revision. Returns metadata of the downloaded file.<\/td>\n<\/tr>\n<tr>\n<th>UploadFile($src_file, $dropbox_path=\u201d)<\/th>\n<td>Uploads a file from webserver to dropbox. Returns metadata of the uploaded file.<\/td>\n<\/tr>\n<tr>\n<th>GetLink($dropbox_file, $preview=true)<\/th>\n<td>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 <a class=\"bafe\" href=\"https:\/\/www.hirzelflyers.ch\/%eb%84%a4%ec%9d%b4%eb%b2%84-%ec%ba%98%eb%a6%b0%eb%8d%94-pc-%eb%8b%a4%ec%9a%b4%eb%a1%9c%eb%93%9c\/\">Naver Calendar pc<\/a>.<\/td>\n<\/tr>\n<tr>\n<th>Delta($cursor)<\/th>\n<td>\u00a0Retrieves file changes. See\u00a0<a href=\"https:\/\/www.dropbox.com\/developers\/reference\/api#delta\">delta<\/a><\/td>\n<\/tr>\n<tr>\n<th>Copy($from_path, $to_path)<\/th>\n<td>\u00a0Copies a file or folder to a new location.<\/td>\n<\/tr>\n<tr>\n<th>CreateFolder($path)<\/th>\n<td>\u00a0Creates a folder.<\/td>\n<\/tr>\n<tr>\n<th>Delete($path)<\/th>\n<td>\u00a0Deletes a file or folder.<\/td>\n<\/tr>\n<tr>\n<th>Move($from_path, $to_path)<\/th>\n<td>\u00a0Moves a file or folder to a new location.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Dropbox REST API Authentication<\/h2>\n<p><a href=\"https:\/\/www.dropbox.com\/developers\/reference\/api\" target=\"_blank\">Dropbox Rest API<\/a>\u00a0uses\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/OAuth\" target=\"_blank\">OAuth<\/a>\u00a0for authentication <a class=\"bafe\" href=\"http:\/\/www.parallelo.it\/windows-xp-iso-%ed%95%9c%ea%b8%80-%eb%8b%a4%ec%9a%b4%eb%a1%9c%eb%93%9c\/\">windows xp iso \ud55c\uae00 \ub2e4\uc6b4\ub85c\ub4dc<\/a>. 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.<\/p>\n<h2>Setup<\/h2>\n<ol>\n<li><a href=\"https:\/\/www.dropbox.com\/developers\/apps\" target=\"_blank\">Create a Dropbox App<\/a><\/li>\n<li>Copy &amp; Paste the App key and secret to the PHP code, that creates the DropboxClient object:<code>$dropbox = new DropboxClient(array('app_key' =&gt; \"YOUR_APP_KEY\",<\/code>\u2018app_secret\u2019 =&gt; \u201cYOUR_APP_SECRET\u201d,\u2019app_full_access\u2019 =&gt; false, \/\/ if the app has full or folder only access),\u2019en\u2019); \/\/ language\/country code<\/li>\n<li>See sample.php. Your web application should run like the following logic (pseudo code):<br \/>\n<code><code>If Then<\/code><\/code>Load Access Token$dropbox-&gt;SetAccessTokenElseIf ThenLoad previously stored request token$dropbox-&gt;GetAccessToken (using the request token)Save Access Token<br \/>\nDelete request token<br \/>\nElse<br \/>\n$dropbox-&gt;GetRequestToken<br \/>\nSave the request token secret<br \/>\n$dropbox-&gt;BuildAuthorizeUrl<br \/>\nRedirect to authorize url<br \/>\nExit<br \/>\nEndIf<br \/>\nDo Dropbox stuff<\/li>\n<\/ol>\n<h2>Tokens<\/h2>\n<p>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:<\/p>\n<p>Array<\/p>\n<p>(<\/p>\n<p>[t] =&gt; 6lj3i20m4fsgi6y<\/p>\n<p>[s] =&gt; 2pn7x2bjsm3xvq8<\/p>\n<p>)<\/p>\n<p>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\u2019s $_SESSION array, then you have have to re-authenticate every new session.<\/p>\n<p>If you get a wrapper not found error, make sure the PHP extension OpenSLL is loaded in php.ini:<\/p>\n<p>extension=php_openssl.dll<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>DropPHP\u00a0provides a simple interface for Dropbox\u2019s REST API to list, download and upload files. For authentication it uses\u00a0OAuthSimple,\u00a0HTTPS requests are made with PHP\u2019s 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\u00a0GitHub page Download the New [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[106],"tags":[],"_links":{"self":[{"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/posts\/614"}],"collection":[{"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/comments?post=614"}],"version-history":[{"count":7,"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/posts\/614\/revisions"}],"predecessor-version":[{"id":623,"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/posts\/614\/revisions\/623"}],"wp:attachment":[{"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/media?parent=614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/categories?post=614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fabi.me\/en\/wp-json\/wp\/v2\/tags?post=614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}