0

Try to access the Dropbox v2 API calls, using PHP and a CURL call. Based on the sandbox here https://dropbox.github.io/dropbox-api-v2-explorer/#files_search, the "Show Code" looks like this.

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

My PHP code looks like this

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

When I execute it, I get

response = {"error": "Not Found"}

3 Answers 3

1

Please try this:

$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";
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response, getting much closer than I was, but this code returns with an error, not sure which part it's talking about -- response = Error in call to API function "files/search": request body: could not decode input as JSON
The error is saying that the JSON in your request body (that is, for your parameters to the API call) is invalid. It looks like you do have an extra pair of \" in there. You probably meant: "{\"path\":\"/My Cars\",\"query\":\".j\",\"start\":0,\"max_results\":50}")
0

Thanks for the pointers guys -- New code - works w/o getting the errors, but now I'm not getting any results returned.... I added some conversions outside the actual call so that I could see what json string were being passed in

Here's the new code

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

Here's what I get for output - no erros BUT no content???? (I've tried different directories, in the sandbox the "/My Cars" returns 6 results)

{"path":"/My Cars","query":".j","start":0,"max_results":50,"mode":{".tag":"filename_and_content"}}

{"body":""}

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

1 Comment

Seems that the sandbox works differently vs the actual API endpoint. Instead of "query":",j" I tried "query":".jpg" and that returned the 6 jpg files I have in that directory. I also tried appending the , mode":{".tag":"filename_and_content"}} but I get an error so I left it out. Hope this thread helps someone that has been having issues with the v2 interface
0

Solved - changing the $query to

$query = ".jpg";

I get results back now.

FYI: no longer required in the code above are the following lines

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

and remove the "echo" function calls to use in production...

Thanks for helping me solve my errors, hopefully this will help someone else

John

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.