doushi7394 2015-06-09 10:13
浏览 32

dropbox php读取文件内容

I am creating a simple dropbox app where after authenticating the user, the app is supposed to prompt for some information, read the content of a txt file. Use ajax for everything after authentication. so far I have managed to authenticate but when I send the info to the server using ajax I am failing to open the file. I have the following server side script:

<?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);

and when I execute it the output is the following:

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

Where the last NULL refers to the content of $fileMetadata. Why is it empty?

the contnet of start.php are the dropbox objects.

<?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);

Edit: in the local files I have a directory called web, where the files executed are. the there is as well a folder (inside web) called todo and the file todo.txt it is inside. __DIR__ refers to inside the web directory that i have mentioned.

In Dropbox i have a direcotry called todo with todo.txt inside.

Edit2: adding the rest of the code. index.php contains:

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

Then dropbox_auth.php has the following code:

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

and finally 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条回答 默认 最新

  • dsjpik057730 2015-06-09 11:14
    关注

    Sorry I cant write a comment right now.

    Try to get the folder/file list by calling $client->getMetadataWithChildren('/todo/') and check if $f is a valid FilePointerRessource :)

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗