dropbox php读取文件内容

时间:2015-06-09 10:13:09

标签: php dropbox dropbox-api readfile

我正在创建一个简单的Dropbox应用程序,在对用户进行身份验证后,应用程序应该提示输入一些信息,读取txt文件的内容。身份验证后,请将ajax用于所有内容。 到目前为止,我已设法进行身份验证,但当我使用ajax将信息发送到服务器时,我无法打开文件。 我有以下服务器端脚本:

<?php
require_once "../app/start.php";

require_once __DIR__.'/../app/config.php';

$regex = $_POST['regex'];
(...)


$accessToken = $_SESSION['accessToken'] ;
$client = new Dropbox\Client($accessToken, $GLOBALS['app_name'], 'UTF-8');
var_dump($client);
// Read File todo.txt

$filename = __DIR__.'/todo/todo.txt';
$f = fopen($filename, "w+b") or die("Unable to open file!");
$fileMetadata = $client->getFile('/todo/todo.txt', $f);
var_dump($fileMetadata);

当我执行它时输出如下:

object(Dropbox\Client)#8 (6) {
  ["accessToken":"Dropbox\Client":private]=>
  string(64) "xxxxxxxxxxxxxxxxxxxxxxxxx"
  ["clientIdentifier":"Dropbox\Client":private]=>
  string(13) "oauth-php/1.0"
  ["userLocale":"Dropbox\Client":private]=>
  string(5) "UTF-8"
  ["apiHost":"Dropbox\Client":private]=>
  string(15) "api.dropbox.com"
  ["contentHost":"Dropbox\Client":private]=>
  string(23) "api-content.dropbox.com"
  ["host"]=>
  object(Dropbox\Host)#5 (3) {
    ["api":"Dropbox\Host":private]=>
    string(15) "api.dropbox.com"
    ["content":"Dropbox\Host":private]=>
    string(23) "api-content.dropbox.com"
    ["web":"Dropbox\Host":private]=>
    string(15) "www.dropbox.com"
  }
}
NULL

最后NULL指的是$fileMetadata的内容。 为何空?

start.php的contnet是保管箱对象。

<?php
session_start();
require_once __DIR__.'/../vendor/autoload.php';
$key = "XCV...";
$secret = "DFG...";
$GLOBALS['app_name'] = "oauth-php/1.0";
$GLOBALS['redirectURI'] = "https://oauth.dev/dropbox_finish.php";
$GLOBALS['HomeURI'] = "https://oauth.dev";

$appInfo = new Dropbox\AppInfo($key, $secret);
$csrfTokenStore = new Dropbox\ArrayEntryStore($_SESSION, 'dropbox-auth-csrf-token');
$webAuth = new Dropbox\WebAuth($appInfo, $GLOBALS['app_name'], $GLOBALS['redirectURI'], $csrfTokenStore);
在本地文件中

编辑:我有一个名为web的目录,其中执行的文件是。{还有一个名为web的文件夹(在todo内),文件todo.txt在里面。 __DIR__指的是我提到的web目录中。

在Dropbox中,我有一个名为todo并且内有todo.txt的目标。

编辑2:添加其余代码。 index.php包含:

<?php 
include_once __DIR__.'/../app/start.php';
include_once __DIR__.'/../app/dropbox_auth.php';

然后dropbox_auth.php包含以下代码:

$authURL = $webAuth->start();
header("Location: $authURL");

最后dropbox_finish.php

<?php
//session_start();
require_once "../app/start.php";

try {
   list($accessToken, $userId, $urlState) = $webAuth->finish($_GET);
   assert($urlState === null);  // Since we didn't pass anything in start()
}
catch (Dropbox\WebAuthException_BadRequest $ex) {
   error_log("/dropbox-auth-finish: bad request: " . $ex->getMessage());
   // Respond with an HTTP 400 and display error page...
}
//review
catch (Dropbox\WebAuthException_BadState $ex) {
   // Auth session expired.  Restart the auth process.
   header('Location: '.$GLOBALS['HomeURI']);
}
catch (Dropbox\WebAuthException_Csrf $ex) {
   error_log("/dropbox-auth-finish: CSRF mismatch: " . $ex->getMessage());
   // Respond with HTTP 403 and display error page...
}
catch (Dropbox\WebAuthException_NotApproved $ex) {
   error_log("/dropbox-auth-finish: not approved: " . $ex->getMessage());
}
catch (Dropbox\WebAuthException_Provider $ex) {
   error_log("/dropbox-auth-finish: error redirect from Dropbox: " . $ex->getMessage());
}
catch (Dropbox\Exception $ex) {
   error_log("/dropbox-auth-finish: error communicating with Dropbox API: " . $ex->getMessage());
}

$client = new Dropbox\Client($accessToken, $GLOBALS['app_name'], 'UTF-8');
$_SESSION['accessToken'] = $accessToken;

$_SESSION['client_object'] = $client;

{... here I load an HTML form that uses AJAX send to `insert.php`}

1 个答案:

答案 0 :(得分:0)

抱歉,我现在无法发表评论。

尝试通过调用$ client-&gt; getMetadataWithChildren(&#39; / todo /&#39;)来获取文件夹/文件列表,并检查$ f是否是有效的FilePointerRessource:)