尝试访问Dropbox v2 API调用,使用PHP和CURL调用。基于这里的沙盒https://dropbox.github.io/dropbox-api-v2-explorer/#files_search,"显示代码"看起来像这样。
curl -X POST https://api.dropboxapi.com/2/files/search
--header 'Authorization: Bearer myAccessTokenHere
--header 'Content-Type: application/json'
--data '{"path":"/My Cars","query":"".j"","start":0,"max_results":50}'
我的PHP代码是这样的
function performSearch()
{
$cheaders = array("Authorization: Bearer myAccessTokenHere",
'Content-Type: application/json',
'Dropbox-API-Arg: {"path":"/My Cars","query":"".j"","start":"0","max_results":"50"}');
$ch = curl_init('https://content.dropboxapi.com/2/files/search');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo "n response = $response";
curl_close($ch);
}
当我执行它时,我得到
response = {"error": "Not Found"}
请试试:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/search");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{"path":"/My Cars","query":"".j"","start":0,"max_results":50}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer myAccessTokenHere",
"Content-Type: application/json"
]);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo "n response = $result";
感谢指针家伙-新代码-工作w/o得到错误,但现在我没有得到任何结果返回....我在实际调用之外添加了一些转换,以便我可以看到
中传递的json字符串。下面是新代码
$path = "/My Cars";
$query = ".j";
$start = 0;
$max_results = 50;
$accessToken = $this->accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/search");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$arr = array('path'=>$path, "query"=>$query, "start"=>$start, "max_results"=>$max_results,
"mode"=>(array(".tag"=>"filename_and_content")));
$jArr = json_encode($arr);
$arrNull = array("body"=>"");
$jNull = json_encode($arrNull);
echo "nn" . $jArr;
echo "nn" . $jNull;
curl_setopt($ch, CURLOPT_POSTFIELDS, $jArr);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $accessToken",
"Content-Type: application/json"
]);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo "n result = $result";
return ($result);
这是我得到的输出-没有错误,但没有内容????(我尝试过不同的目录,在沙箱中"/My Cars"返回6个结果)
{"路径":"/我的汽车"、"查询":" . ","开始":0,"max_results":50岁"模式":{".tag":"filename_and_content}}
{"身体":"}
结果={"匹配":[],"更多":假的,"开始":6}
已解决-将$查询更改为
$query = ".jpg";
我现在得到结果了。
仅供参考:以上代码中不再需要以下行
$arrNull = array("body"=>");$jNull = json_encode($arrNull);
并删除"echo"函数调用,以便在生产环境中使用…
谢谢你帮我解决我的错误,希望这将帮助别人
约翰