搜索文件没有找到

时间:2015-02-20 10:39:56

标签: php dropbox-api

我正在尝试搜索(过滤)Dropbox文件夹中的文件,但是当存在与过滤器匹配的文件时,没有找到任何文件。我没有使用Dropbox提供的PHP库。

以下是代码摘录:

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;
    }

}

然后我调用Dropbox的GetFolder方法:

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

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

当我将$filter传递给GetFolder时,它使用search/sandbox路径并创建一个参数数组($params),其中包含所需的query参数。

如果我没有向GetFolder提供$fileFilter参数并且文件夹中的所有文件都已返回(使用metadata/sandbox路径),则此过程正常。

Dropbox类的其他方法(简洁摘录中没有)使用$params功能,它们可以正常工作。

我一直在使用Dropbpox API参考作为指导(https://www.dropbox.com/developers/core/docs#search

1 个答案:

答案 0 :(得分:0)

乍一看,您似乎正在向GET发出/search请求,但通过CURLOPT_POSTFIELDS传递参数。尝试使用POST或将搜索查询编码为查询字符串参数。

修改

下面是一些适合我的代码(用法:php search.php <term>)。请注意,我使用OAuth 2代替OAuth 1,因此我的Authorization标题与您的标题不同。

<?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:\n\t" . join("\n\t",
    array_map(function ($file) {
        return $file['path'];
    }, json_decode($api_response, true)))."\n";

?>