Поиск файлов Ничего не найдено

Я пытаюсь найти (отфильтровать) файлы в папке Dropbox, но при обнаружении файлов, соответствующих этому фильтру, файлы не обнаруживаются. Я не использую библиотеку PHP, предоставляемую Dropbox.

Вот выдержка из кода:

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

}

Затем я вызываю метод GetFolder Dropbox как таковой:

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

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

Как я прохожу $filter в GetFolder, он использует search/sandbox путь и создает массив параметров ($params) с необходимыми query параметр в нем.

Процесс работает нормально, если я не предоставлю $fileFilter параметр GetFolder и все файлы в папке возвращаются (использует metadata/sandbox дорожка).

Другие методы (которые не включены в выдержку для краткости) класса Dropbox используют $params особенность и они работают нормально.

Я использовал ссылку на API Dropbpox для руководства (https://www.dropbox.com/developers/core/docs#search)

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

?>
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector