dsvd407787736 2015-02-20 10:39
浏览 25
已采纳

搜索文件没有找到

I am trying to search (filter) for files in a Dropbox folder, but no files are being found when there are files that match the filter. I am not using the PHP library provided by Dropbox.

Here is an extract of the code:

class Dropbox {

    private $headers = array();
    private $authQueryString = "";

    public $SubFolders = array();
    public $Files = array();

    function __construct() {
        $this->headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.DROPBOX_APP_KEY.'", oauth_token="'.DROPBOX_OAUTH_ACCESS_TOKEN.'", oauth_signature="'.DROPBOX_APP_SECRET.'&'.DROPBOX_OAUTH_ACCESS_SECRET.'"');
        $this->authQueryString = "oauth_consumer_key=".DROPBOX_APP_KEY."&oauth_token=".DROPBOX_OAUTH_ACCESS_TOKEN."&oauth_signature_method=PLAINTEXT&oauth_signature=".DROPBOX_APP_SECRET."%26".DROPBOX_OAUTH_ACCESS_SECRET."&oauth_version=1.0";
    }

    public function GetFolder($folder, $fileFilter = "") {
        //Add the required folder to the end of the base path for folder call
        if ($fileFilter == "")
            $subPath = "metadata/sandbox";
        else
            $subPath = "search/sandbox";
        if (strlen($folder) > 1) {
            $subPath .= (substr($folder, 0, 1) != "/" ? "/" : "")
                .$folder;
        }

        //Set up the post parameters for the call
        $params = null;
        if ($fileFilter != "") {
            $params = array(
                "query" => $fileFilter
            );
        }

        //Clear the sub folders and files logged
        $this->SubFolders = array();
        $this->Files = array();

        //Make the call
        $content = $this->doCall($subPath, $params);

        //Log the files and folders
        for ($i = 0; $i < sizeof($content->contents); $i++) {
            $f = $content->contents[$i];
            if ($f->is_dir == "1") {
                array_push($this->SubFolders, $f->path);
            } else {
                array_push($this->Files, $f->path);
            }
        }

        //Return the content
        return $content;
    }

    private function doCall($urlSubPath, $params = null, $filePathName = null, $useAPIContentPath = false) {
        //Create the full URL for the call
        $url = "https://api".($useAPIContentPath ? "-content" : "").".dropbox.com/1/".$urlSubPath;

        //Initialise the curl call
        $ch = curl_init();

        //Set up the curl call
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($params != null)
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        $fh = null;
        if ($filePathName != null) {
            $fh = fopen($filePathName, "rb");
            curl_setopt($context, CURLOPT_BINARYTRANSFER, true);
            curl_setopt($context, CURLOPT_INFILE, $fh);
            curl_setopt($context, CURLOPT_INFILESIZE, filesize($filePathName));
        }

        //Excecute and get the response
        $api_response = curl_exec($ch);

        if ($fh != null)
            fclose($fh);

        //Process the response into an array
        $json_response = json_decode($api_response);

        //Has there been an error
        if (isset($json_response->error )) {
            throw new Exception($json_response["error"]);
        }

        //Send the response back
        return $json_response;
    }

}

I then call the GetFolder method of Dropbox as such:

$dbx = new Dropbox();
$filter = "MyFilter";
$dbx->GetFolder("MyFolder", $filter);

print "Num files: ".sizeof($dbx->Files);

As I am passing $filter into GetFolder, it uses the search/sandbox path and creates a parameter array ($params) with the required query parameter in it.

The process works fine if I don't provide the $fileFilter parameter to GetFolder and all files in the folder are returned (uses the metadata/sandbox path).

Other methods (that are not in the extract for brevity) of the Dropbox class use the $params feature and they to work fine.

I have been using the Dropbpox API reference for guidance (https://www.dropbox.com/developers/core/docs#search)

  • 写回答

1条回答 默认 最新

  • douyan1944 2015-02-20 16:43
    关注

    At first glance, it looks like you're making a GET request to /search but passing parameters via CURLOPT_POSTFIELDS. Try using a POST or encoding the search query as a query string parameter.

    EDIT

    Below is some code that works for me (usage: php search.php <term>). Note that I'm using OAuth 2 instead of OAuth 1, so my Authorization header looks different from yours.

    <?php
    
    $access_token = '<REDACTED>';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.dropbox.com/1/search/auto');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:Bearer ' . $access_token));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('query' => $argv[1]));
    
    $api_response = curl_exec($ch);
    
    echo "Matching files:
    \t" . join("
    \t",
        array_map(function ($file) {
            return $file['path'];
        }, json_decode($api_response, true)))."
    ";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败