title: How to get files and folders from dropbox using php API
body: <p>I am using below files for dropbox login .</p>
  
  <p>index.php</p>
  
  <pre><code>&lt;?php require_once 'Dropbox.class.php';
  $dropbox = new Dropbox();
  $userArray = $dropbox-&gt;get('https://api.dropbox.com/1/account/info');  
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Dropbox API&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;Dropbox API&lt;/h1&gt;
  &lt;?php 
  if($dropbox-&gt;hasAccess()){  
  $result = json_decode($userArray);
  echo '
  &lt;h2&gt;User Info&lt;/h2&gt;
  &lt;ul&gt;    
  &lt;li&gt;'.$result-&gt;display_name.'&lt;/li&gt;
  &lt;li&gt;'.$result-&gt;email.'&lt;/li&gt;
  &lt;/ul&gt; 
  &lt;form action="get.php" method="GET"&gt;
  &lt;label&gt;File name to get:&lt;/label&gt;
  &lt;input type="text" name="path"/&gt;
  &lt;input type="submit" /&gt;
  ';
  }
  else{
  echo '
  &lt;h2&gt;Login&lt;/h2&gt;
  &lt;a href="'.$dropbox-&gt;getAccessURL().'"&gt;Login to Dropbox&lt;/a&gt;';
  }
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Using index.php code i can login easily and also i can redirect to same file after authetication.but how can list all the dropbox files and how to upload files to dropbox</p>
  
  <p>get.php</p>
  
  <pre><code>&lt;?php 
  require_once 'Dropbox.class.php';  
  $dropbox = new Dropbox(); //create new dropbox object
  $root = 'sandbox'; //set to the root either: 'sandbox' or 'dropbox'
  $path = $_GET['path']; //path from the form
  //using the GET method
  echo 'Contents of: https://api-content.dropbox.com/1/files/'.$root.'/'.$path.'&lt;br&gt;';
  echo $dropbox-&gt;get('https://api-content.dropbox.com/1/files/sandbox/test.txt'); // cUrl GET call from dropbox object
  //this code uses the getFile method
  echo '&lt;br&gt;&lt;br&gt;'; //line space
  echo 'Contents of: '.$path.'&lt;br&gt;';
  echo $dropbox-&gt;getFile($root,'test.txt');// getFile method from Dropbox object
  ?&gt;
  </code></pre>
  
  <p>Dropbox.class.php</p>
  
  <pre><code>&lt;?php
  session_start();
  
  /**
   * Dropbox API
   *
   * This is a simple PHP plaintext OAuth 1.0 API for Dropbox
   * 
   * @author Sean Thomas Burke &lt;http://www.seantburke.com/&gt;
   */
  
  class Dropbox
  {
      //app variables, be sure to set these to your app settings before continuing.
      //they can be found at https://www.dropbox.com/developers/apps
      private static $APP_KEY         = 'xxxxxx';
      private static $APP_SECRET      = 'xxxxxx';
      private static $CALLBACK_URL        = 'URL';
  
      //OAuth 1.0 variables
      private $request_token_url;     //url to dropbox.com to get authorization
      private $oauth_token_secret;        //reponse secret from initial request
      private $oauth_request_token;       //initial response token
      private $oauth_access_token;        //store this in your database
      private $oauth_signature;       //store this in your database, they need to be used in every API call
      private $uid;               //the uid returned as $_GET['uid']
  
      /**
       * Dropbox() 
       * creates the object and decides based on the $_SESSION whether to request() or processCallBack()
       *
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com/&gt;
       */
      public function __construct()
      {   
          //store session variables from a request()
          //this won't do anything until the processCallBack() method is called after the request()
          $this-&gt;oauth_token_secret   = $_SESSION['oauth_token_secret'];
          $this-&gt;oauth_request_token  = $_SESSION['oauth_request_token'];
          $this-&gt;oauth_access_token   = $_SESSION['oauth_access_token'];
          $this-&gt;oauth_signature      = $_SESSION['oauth_signature'];
          $this-&gt;uid          = $_SESSION['uid'];     
  
          //if the required variables are not set, then decide whether to make a request or process the $_SESSION
          if(!$this-&gt;oauth_signature || !$this-&gt;oauth_access_token)
          {
              //if the following are not set, then a request needs to be made
              //the fallback decision should be to request for a new token, and not to process the callback method
              if($_GET['uid'] &amp;&amp; $_GET['oauth_token'] &amp;&amp; $this-&gt;oauth_token_secret &amp;&amp; $this-&gt;oauth_request_token)
              {
                  $this-&gt;processCallBack();
              }
              else
              {
                  $this-&gt;request();
              }
          }
      }
  
      /**
       * request() 
       *
       * sends a request to get OAuth request token and secret, builds the request_token_url
       * Step 1: call for request
       * @link https://www.dropbox.com/developers/reference/api#request-token
       * @link https://www.dropbox.com/developers/reference/api#authorize
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com/&gt;
       */
      private function request()
      {
          // initiate a cURL; if you don't know what curl is, look it up at http://curl.haxx.se/
          $ch = curl_init(); 
          //Dropbox uses plaintext OAuth 1.0; make the header for this request
          $headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_signature="'.self::$APP_SECRET.'&amp;"');  
          // set cURL options and execute
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
          curl_setopt($ch, CURLOPT_URL, "https://api.dropbox.com/1/oauth/request_token");  
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
          $request_token_response = curl_exec($ch);
  
          //  parse the returned data which has the format:
          // "oauth_token=&lt;access-token&gt;&amp;oauth_token_secret=&lt;access-token-secret&gt;"
          parse_str($request_token_response, $parsed_request_token);
  
          //check for any errors
          $json_access = json_decode($request_token_response);
          if($json_access-&gt;error)
          {
              echo '&lt;br&gt;&lt;br&gt;FATAL ERROR: '.$json_access-&gt;error.'&lt;br&gt;&lt;br&gt;';
          }
  
          //set these variables in a $_SESSION variable
          $_SESSION['oauth_token_secret']     = $parsed_request_token['oauth_token_secret'];
          $_SESSION['oauth_request_token']    = $parsed_request_token['oauth_token'];
  
          //also store them in the object (unnecessary, but helps understand concept)
          $this-&gt;oauth_token_secret       = $parsed_request_token['oauth_token_secret'];
          $this-&gt;oauth_request_token      = $parsed_request_token['oauth_token'];
  
          //get the request URL; this is where you send the user to authorize your request. Be sure to set the CALLBACK_URL before doing this.
          $this-&gt;request_token_url = 'https://www.dropbox.com/1/oauth/authorize?oauth_token='.$parsed_request_token['oauth_token'].'&amp;oauth_callback='.self::$CALLBACK_URL;    
  
      }
  
      /**
       * processCallBack() 
       * 
       * call this function when the user returns from the request_token_url at dropbox.com 
       * Step 2: Process Request and get Signature and Access Token
       * @link    https://www.dropbox.com/developers/reference/api#request-token
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com&gt;
       */
      private function processCallBack()
      {
          //Now we must process the request 
          //same steps as before, but now the header is modified to include the response variables that were stored in the session
          //notice the signature is a concatenation of the app_secret and the token_secret
          $ch = curl_init();  
          $headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_token="'.$this-&gt;oauth_request_token.'", oauth_signature="'.self::$APP_SECRET.'&amp;'.$this-&gt;oauth_token_secret.'"');  
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
          curl_setopt($ch, CURLOPT_URL, "https://api.dropbox.com/1/oauth/access_token");  
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
  
          //execute and parse
          $access_token_response = curl_exec($ch);  
          parse_str($access_token_response, $parsed_access_token);
  
          //check for errors
          $json_access = json_decode($access_token_response);
          if($json_access-&gt;error)
          {
              echo '&lt;br&gt;&lt;br&gt;FATAL ERROR: '.$json_access-&gt;error.'&lt;br&gt;&lt;br&gt;';
          }
  
          //it is unnecessary to keep the oauth_token_secret and oauth_request_token at this point
          //clear the $_SESSION
          session_unset();
  
          //store oauth_access_token and oauth_signature responses in $_SESSION
          //again, oauth_signature is a concatenation of the APP_SECRET and the oauth_token_secret response
          //these 2 variables are what you need to make API requests
          $_SESSION['oauth_access_token']     = $parsed_access_token['oauth_token'];
          $_SESSION['oauth_signature']        = self::$APP_SECRET.'&amp;'.$parsed_access_token['oauth_token_secret']; 
  
          //dropbox also gives you uid, store it
          $_SESSION['uid']            = $_GET['uid'];
  
          //also store variables in the object for future reference
          $this-&gt;oauth_access_token       = $parsed_access_token['oauth_token'];
          $this-&gt;oauth_signature          = self::$APP_SECRET.'&amp;'.$parsed_access_token['oauth_token_secret']; 
          $this-&gt;uid              = $_GET['uid'];
      }
  
      /**
       * get($url)
       *
       * Using the REST api, make a call to a REST URL, and it will return the array
       * Step 3: Make an API call
       *
       * @link    https://www.dropbox.com/developers/reference/api    
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com&gt;
       *
       * @param   $url    REST URL        
       * @return  array   decoded from JSON response
       */
      function get($url)
      {
          $ch = curl_init(); 
          $headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_token="'.$this-&gt;oauth_access_token.'", oauth_signature="'.$this-&gt;oauth_signature.'"');  
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
          curl_setopt($ch, CURLOPT_URL, $url);  
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          $api_response = curl_exec($ch);
          return $api_response;
      }
  
      /**
       * call($url)
       *
       * Using the REST api, make a call to a REST URL, and it will return the array
       * Step 3: Make an API call
       *
       * @link    https://www.dropbox.com/developers/reference/api    
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com&gt;
       *
       * @param   $url    REST URL        
       * @return  array   decoded from JSON response
       */
      function put($url)
      {
          $ch = curl_init(); 
          $headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_token="'.$this-&gt;oauth_access_token.'", oauth_signature="'.$this-&gt;oauth_signature.'"');  
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
          curl_setopt($ch, CURLOPT_URL, $url);  
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
          $api_response = curl_exec($ch);
          return json_decode($api_response);
      }
  
  
      /**
       * getAccessURL() 
       * returns the URL used for requests
       * @link    https://www.dropbox.com/developers/reference/api#authorize
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com&gt;
       *
       * @return  string of URL for requesting OAuth Token
       */
      function getAccessURL()
      {
          //get the Request URL
          return $this-&gt;request_token_url;
      }
  
      /**
       * hasAccess() 
       * check to see if the user has access already
       *
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com&gt;
       * @return  boolean of 3 required variables (uid is not required, but it helps)
       */
      function hasAccess()
      {
          return ($this-&gt;oauth_access_token &amp;&amp; $this-&gt;oauth_signature &amp;&amp; $this-&gt;uid);
      }
  
  
      /**
       * getFile()
       * get a file from the dropbox
       * @link    https://www.dropbox.com/developers/reference/api#files-GET
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com&gt;
       *
       * @param   $root {sandbox, dropbox} $path {url path to document}
       * @return  files contents
       */
      function getFile($root, $path)
      {
          //return $this-&gt;get('https://api-content.dropbox.com/1/files/'.$root.'/'.$path);
          return $this-&gt;get('https://api.dropbox.com/1/files/'.$root.'/'.$path);
      }
  
      /**
       * putFile 
       * get a file from the dropbox
       * @link    https://www.dropbox.com/developers/reference/api#files-GET
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com&gt;
       *
       * @param   $root {sandbox, dropbox} $path {url path to document}
       * @return  boolean of 3 required variables (uid is not required, but it helps)
       */
      function putFile($root,$path)
      {
          //TODO still needs implementation
          return $this-&gt;put('https://api-content.dropbox.com/1/files_put/'.$root.'/'.$path.'?param=val');
      }
  
  
      /**
       * test() 
       * test the dropbox upload function
       * @link    https://www.dropbox.com/developers/reference/api#files-GET
       * @author  Sean Thomas Burke &lt;http://www.seantburke.com&gt;
       *
       * @param   $root either "sandbox" or "dropbox" $path {url path to document}
       * @return  boolean of 3 required variables (uid is not required, but it helps)
       */
      function test($root)
      {
          $url = 'https://api-content.dropbox.com/1/files_put/'.$root.'/test2.txt?overwrite=true&amp;locale=en';
          $body = file_get_contents('test.txt');
          //echo "file_contents: ".$body;
          $fp = fopen('php://temp/maxmemory:256000', 'w');
          if (!$fp) {
              die('could not open temp memory data');
          }
          fwrite($fp, $body);
          fseek($fp, 0); 
  
          $ch = curl_init(); 
          $headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_token="'.$this-&gt;oauth_access_token.'", oauth_signature="'.$this-&gt;oauth_signature.'"');  
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
          //echo '&lt;a href="'.$url.'"&gt;'.$url.'&lt;/a&gt;';
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
          curl_setopt($ch, CURLOPT_INFILE, $fp); // file pointer
          curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));  
  
          return $api_response = curl_exec($ch);
      }
  
  }
  </code></pre>