2014-01-14 41 views
0

我想創建一個頁面,以顯示的沙箱時尚的形式顯示保管箱文件夾的內容,並允許瀏覽用戶(誰登錄到我一直在開發的網站)能夠點擊並下載文件夾中的各種文件。 這裏是我使用的代碼: 這是文件的bootstrap.php ...實現dropbox API到php的問題

<?php 
// Prevent calling this script directly 
if ($_SERVER["SCRIPT_FILENAME"] == __FILE__) { 
    exit("Access denied!"); 
} 

// app settings 
$config = array(); 
$config["dropbox"]["app_key"] = "***"; 
$config["dropbox"]["app_secret"] = "***"; 
// ACCESS_TYPE should be "dropbox" or "app_folder" 
$config["dropbox"]["access_type"] = "dropbox"; 

$config["app"]["root"] = ((!empty($_SERVER["HTTPS"])) ? "https" : "http") . "://" . $_SERVER["HTTP_HOST"] . "/"; 
$config["app"]["datadir"] = dirname(__FILE__) . "/data"; 
$config["app"]["authfile"] = $config["app"]["datadir"] . "/auth.php"; 

// turn on error reporting for development 
error_reporting(E_ALL|E_STRICT); 
ini_set("display_errors", true); 

// environment check 
if (!is_dir($config["app"]["datadir"]) || !is_writable($config["app"]["datadir"])) { 
    exit("The data directory is not writeable!"); 
} 
if (file_exists($config["app"]["authfile"]) && !is_writable($config["app"]["authfile"])) { 
    exit("The auth storage file is not writeable!"); 
} 

// Load libraries and start a new session 
require_once "lib/dropbox/rest.php"; 
require_once "lib/dropbox/session.php"; 
require_once "lib/dropbox/client.php"; 

if(!isset($_SESSION)){session_start();} 

// Search for a previously obtained access token 
$access_token = null; 
if (file_exists($config["app"]["authfile"])) { 
    include_once $config["app"]["authfile"]; 
} 

這是文件authorize.php ...

<?php 
require_once "bootstrap.php"; 

if (isset($access_token)) { 
    header("Location: ./"); 
    exit; 
} 

try { 
    // Start a new Dropbox session 
    $session = new DropboxSession(
     $config["dropbox"]["app_key"], 
     $config["dropbox"]["app_secret"], 
     $config["dropbox"]["access_type"] 
    ); 

    // The user is redirected here by Dropbox after the authorization screen 
    if (!empty($_GET["oauth_token"]) && !empty($_GET["uid"])) { 
     $uid = $_GET["uid"]; 
     $token = array(
      "oauth_token" => $_GET["oauth_token"], 
      "oauth_token_secret" => "" 
     ); 

     if (!empty($_SESSION["request_token"])) { 
      $token["oauth_token_secret"] = $_SESSION["request_token"]["oauth_token_secret"]; 
     } 

     /** 
     * The access token is all you'll need for all future API requests on 
     * behalf of this user, so you should store it away for safe-keeping 
     * (even though we don't for this article). By storing the access 
     * token, you won't need to go through the authorization process again 
     * unless the user revokes access via the Dropbox website. 
     */ 
     if ($access_token = $session->obtainAccessToken($token)) { 
      parse_str($access_token, $token); 
      $access_token = $token; 
      unset($token); 

      // The output ov var_export is similar to: 
      // array("oauth_token_secret" => "aaaa", "oauth_token" => "bbbb", "uid" => "123456") 
      $data = '<?php $access_token = ' . var_export($access_token, true) . ";"; 
      if (file_put_contents($config["app"]["authfile"], $data) === false) { 
       throw new Exception("Unable save access token"); 
      } 

      // Authorized, redirect to index 
      //header("Location: index_inside.php"); 
      echo "Authorized, <a href=\"list.php\">click here</a> to redirect!"; 
      exit; 
     } 
     // The access token should be stored somewhere to be reused until 
     // it expires or is revoked by the user 
    } 
    else { 
     // We must start a new authorization cycle 
     if ($request_token = $session->obtainRequestToken()) { 
      // The request token must be subdivided in the two components 
      // oauth_token_secret and oauth_token and kept in the session 
      // because is needed in the next step 
      parse_str($request_token, $token); 
      $_SESSION["request_token"] = $token; 

      $url = $session->buildAuthorizeURL(
       $token, 
       $config["app"]["root"] . basename($_SERVER["SCRIPT_NAME"]), 
       "en-US"); 

      // Display or redirect to auth URL 
      echo '<p>Please visit <a href="' . $url . '">Dropbox</a> and authorize this application.</p>'; 
      exit; 
     } 
     else { 
      throw new Exception("Unable to get request token"); 
     } 
    } 
} 
catch (Exception $e) { 
    echo $e->getMessage(); 
} 

這是文件list_inside .PHP ......這就是最終被列入提出的文件夾列表(理想與下載鏈接文件)...

<?php 
require_once "bootstrap.php"; 

if (!isset($access_token)) { 
    header("Location: authorize.php"); 
    exit; 
} 

try { 
    // Start a new Dropbox session 
    // The access token should be defined 
    // The session should verify if the token is valid and throw an exception 
    $session = new DropboxSession(
     $config["dropbox"]["app_key"], 
     $config["dropbox"]["app_secret"], 
     $config["dropbox"]["access_type"], 
     $access_token 
    ); 
    $client = new DropboxClient($session); 

    $path = (!empty($_GET["path"])) ? $_GET["path"] : "/Apps/Tools/"; 

    // List contents of home directory 
    if ($home = $client->metadata($path)) { 
     echo "<p>Metadata content for <code>" . $path . "</code></p>"; 
     echo "<pre>" . print_r($home, true) . "</pre>"; 
    } 
} 
catch (Exception $e) { 
    echo "<strong>ERROR (" . $e->getCode() . ")</strong>: " . $e->getMessage(); 
    if ($e->getCode() == 401) { 
     // Remove auth file 
     unlink($config["app"]["authfile"]); 
     // Re auth 
     echo '<p><a href="authorize.php">Click Here to re-authenticate</a></p>'; 
    } 
} 

下面是使用metada上面的代碼輸出TA():

元數據內容/應用程序/工具/陣列( [散列] => fa7f3577894553ffeb70ac0d96e49b99 [修改] => 71425 [轉] => 1170104ef29f8 [thumb_exists] => [字節] => 0 [修改] => 2014年1月14日星期二03:10:05 +0000 [路徑] =>/Apps /工具 [is_dir] => 1 [圖標] =>文件夾 [root] => dropbox [contents] =>陣列 ( [0] =>陣列 ( [修改] => 71426 [rev] => 1170204ef29f8 [thumb_exists] => [bytes] => 0 [修改] => 2014年1月14日星期二03:10:05 +0000 [路徑] => /應用程序/工具/伯恩賽德道幹溪谷赤霞珠 [is_dir] => 1 [ICON] =>夾 [根] =>保管箱 [尺寸] => 0字節 )

 [1] => Array 
      (
       [revision] => 71436 
       [rev] => 1170c04ef29f8 
       [thumb_exists] => 
       [bytes] => 0 
       [modified] => Tue, 14 Jan 2014 03:10:05 +0000 
       [path] => /Apps/Tools/Burnside Road Dry Creek Valley Sauvignon Blanc 
       [is_dir] => 1 
       [icon] => folder 
       [root] => dropbox 
       [size] => 0 bytes 
      ) 

     [2] => Array 
      (
       [revision] => 71445 
       [rev] => 1171504ef29f8 
       [thumb_exists] => 
       [bytes] => 0 
       [modified] => Tue, 14 Jan 2014 03:10:05 +0000 
       [path] => /Apps/Tools/Burnside Road Mendocino County Zinfandel 
       [is_dir] => 1 
       [icon] => folder 
       [root] => dropbox 
       [size] => 0 bytes 
      ) 

     [3] => Array 
      (
       [revision] => 71454 
       [rev] => 1171e04ef29f8 
       [thumb_exists] => 
       [bytes] => 0 
       [modified] => Tue, 14 Jan 2014 03:10:05 +0000 
       [path] => /Apps/Tools/Burnside Road Pinot Noir California 
       [is_dir] => 1 
       [icon] => folder 
       [root] => dropbox 
       [size] => 0 bytes 
      ) 



    ) 

[size] => 0 bytes) 

我爲波濤洶涌的代碼表示歉意,我對此並不是非常熟練,但是,我的朋友ne編輯幫助網站,我跳下來幫助讓Dropbox工作。

問題是......它顯示有關文件夾的信息數組,但沒有文件可通過鏈接進行下載。

新增info..here是另一個頁面的代碼我嘗試:(我列出輸出這正下方的PHP)

<?php 
require_once "bootstrap.php"; 

if (!isset($access_token)) { 
    header("Location: authorize.php"); 
    exit; 
} 

try { 
    // Start a new Dropbox session 
    // The access token should exist 
    // The session should verify if the token is valid and throw an exception 
    $session = new DropboxSession(
     $config["dropbox"]["app_key"], 
     $config["dropbox"]["app_secret"], 
     $config["dropbox"]["access_type"], 
     $access_token 
    ); 

    $client = new DropboxClient($session); 
    $path = (!empty($_GET["path"])) ? $_GET["path"] : "/webs.pdf"; 
    $dest = $config["app"]["datadir"] . "/" . basename($path); 

    // Download a file 
    if ($file = $client->getFile($path, $dest)) { 

     if (!empty($dest)) { 
      unset($file["data"]); 
      echo "<p>File saved to: <code>" . $dest . "</code></p>"; 
      echo "<pre>" . print_r($file, true) . "</pre>"; 
     } 
     else { 
      header("Content-type: " . $file["mime"]); 
      echo $file["data"]; 
      exit; 
     } 
    } 
} 
catch (Exception $e) { 
    echo "<strong>ERROR (" . $e->getCode() . ")</strong>: " . $e->getMessage(); 
    if ($e->getCode() == 401) { 
     // Remove auth file 
     unlink($config["app"]["authfile"]); 
     // Re auth 
     echo '<p><a href="authorize.php">Click Here to re-authenticate</a></p>'; 
    } 
} 

這裏是上面代碼的輸出:

文件保存到:/home/thisisyo/public_html/data/webs.pdf Array( [name] =>/home/thisisyo/public_html/data/webs。PDF [MIME] =>應用/ PDF [間位] => stdClass的對象 ( [修改] => 35075 [轉] => 890304ef29f8 [thumb_exists] => [字節] => 703289 [改性] =>四月20,2013 23:39:10 +0000 [client_mtime] => 2013年2月20日星期三19:19:42 +0000 [路徑] => /webs.pdf [is_dir] => [圖標] => page_white_acrobat [根] =>保管箱 [MIME_TYPE] =>應用/ PDF [尺寸] => 686.8 KB )

而且,這裏是client.php定義的類:

public function metadata($path, $list = true, $fileLimit = 10000, $hash = null, $revision = null, $includeDeleted = false) { 
    // Prepare argument list 
    $args = array(
     "file_limit" => $fileLimit, 
     "hash" => $hash, 
     "list" => (int) $list, 
     "include_deleted" => (int) $includeDeleted, 
     "rev" => $revision 
    ); 

    // Prepend the right access string to the desired path 
    if ("dropbox" == $this->accessType) { 
     $path = "dropbox" . $path; 
    } 
    else { 
     $path = "sandbox" . $path; 
    } 

    // Execute 
    $response = $this->Session->fetch("GET", $this->dropboxAPIURL, "/metadata/" . $path, $args); 
    return $response["body"]; 
} 

這是的GetFile()類中定義...

公共職能的GetFile($路徑, $不過outFile = NULL,$修訂= NULL){

$args = array(); 
if (!empty($revision)) { 
    $args["rev"] = $revision; 
} 

// Prepend the right access string to the desired path 
if ("dropbox" == $this->accessType) { 
    $path = "dropbox" . $path; 
} 
else { 
    $path = "sandbox" . $path; 
} 

// Get the raw response body 
$response = $this->Session->fetch("GET", $this->dropboxContentAPIURL, "/files/" . $path, $args, true); 

if ($outFile != null) { 
    if (file_put_contents($outFile, $response["body"]) === false) { 
     throw new Exception("Unable to write file '$outfile'"); 
    } 
} 

return array(
    "name" => ($outFile) ? $outFile : basename($path), 
    "mime" => $response["headers"]["content-type"], 
    "meta" => json_decode($response["headers"]["x-dropbox-metadata"]), 
    "data" => $response["body"] 
); 

}

+0

我要補充一點,這個代碼不工作,它吐出來的內容的完整列表,文件夾,應用/工具/從我的Dropbox,但問題是我想要可喜的sandboxy類型的輸出,點擊鏈接。 – user2990466

+0

你能發佈一個示例輸出嗎?您需要編寫代碼以循環訪問數組。 –

+0

我也試過添加的代碼,我會發布代碼輸出是的。謝謝你BTW JOSH – user2990466

回答

0

對於你的list_inside.php頁面,你所需要做的就是遍歷你的$client->metadata()數組並打印HTML。下面是該頁面的示例:

<?php 

require_once "bootstrap.php"; 

if (!isset($access_token)) { 
    header("Location: authorize.php"); 
    exit; 
} 

try { 
    // Start a new Dropbox session 
    // The access token should be defined 
    // The session should verify if the token is valid and throw an exception 
    $session = new DropboxSession(
     $config["dropbox"]["app_key"], 
     $config["dropbox"]["app_secret"], 
     $config["dropbox"]["access_type"], 
     $access_token 
    ); 
    $client = new DropboxClient($session); 

    $path = (!empty($_GET["path"])) ? $_GET["path"] : "/Apps/Tools/"; 

    // List contents of home directory 
    if ($home = $client->metadata($path)) { 
     echo <<<EOF 
     <h1>Index of $index</h1> 

     <table> 
      <thead> 
       <tr> 
        <th>Name</th> 
        <th>Last Modified</th> 
        <th>Size</th> 
        <th>Type</th> 
       </tr> 
      </thead> 
      <tbody> 
     EOF; 

     foreach($home as $list) { 
      $link = ($list[is_dir] == 1 ? "list_inside" : "download").".php?path=".$list[path]; 
      $file = explode("/", $list[path]); 
      $path = $file[count($file)-1]; 
      $size = ($list[bytes] == 0 ? "-" : $list[size]); 

      echo <<<EOF 
       <tr> 
        <td><a href="$link">$path</a></td> 
        <td>$list[modified]</td> 
        <td>$size</td> 
        <td>$list[type]</td> 
       </tr> 
      EOF; 
     } 

     echo <<<EOF 
      </tbody> 
     </table> 
     EOF; 
    } 
} catch (Exception $e) { 
    echo "<strong>ERROR (" . $e->getCode() . ")</strong>: " . $e->getMessage(); 
    if ($e->getCode() == 401) { 
     // Remove auth file 
     unlink($config["app"]["authfile"]); 
     // Re auth 
     echo '<p><a href="authorize.php">Click Here to re-authenticate</a></p>'; 
    } 
} 

?> 
0

您只需調用一個函數即可下載該文件。

require_once 'dropbox/DropboxClient.php'; 
              $dropbox = new DropboxClient(array(
                'app_key' => DROPBX_API_KEY, 
                'app_secret' => DROPBX_API_SECRET, 
                'app_full_access' => TRUE, 
             ),'en'); 
$fileMetadata = $dropbox->DownloadFile($value->path,'download/'.$file); 

檢查這一點,如果它可以幫助你