PHP CURL调用的Dropbox v2 API问题无法正常工作

时间:2016-07-02 02:17:09

标签: api dropbox

尝试使用PHP和CURL调用访问Dropbox v2 API调用。基于这里的沙箱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);
}

当我执行它时,我得到了

  

响应= {"错误":"未找到"}

3 个答案:

答案 0 :(得分:1)

请试试这个:

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

答案 1 :(得分:0)

感谢指点人员 - 新代码 - 没有得到错误,但现在我没有得到任何结果返回....我在实际调用之外添加了一些转换,以便我可以看到什么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 "\n\n" . $jArr;
echo "\n\n" . $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);

这是我得到的输出 - 没有错误但没有内容???? (我尝试了不同的目录,在沙盒中“/我的汽车”返回6个结果)

{“path”:“/ My Cars”,“query”:“。j”,“start”:0,“max_results”:50,“mode”:{“。tag”:“filename_and_content”}}

{ “本体”: “”}

result = {“matches”:[],“more”:false,“start”:6}

答案 2 :(得分:0)

已解决 - 将$查询更改为

$ query =" .jpg&#34 ;;

我现在收到了结果。

仅供参考:上述代码中不再需要以下行

$ arrNull = array(" body" =>""); $ jNull = json_encode($ arrNull);

并删除" echo"函数调用在生产中使用......

感谢您帮助我解决错误,希望这会帮助其他人

约翰