Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

Priya M.'s avatar
Priya M.
Explorer | Level 4
8 years ago

Dropbox API for search in PHP

Hi,

 

I'm trying to search for a file uploaded in my Dropbox APP using the following code:

 

 

$data = json_encode(array("path" => "/", "query" => $this->filename, "mode" => "filename"));
$url = 'https://api.dropboxapi.com/2/files/search';
$headers = array('Authorization: Bearer '.$GLOBALS['access_token'],
'data: '.$data,
'Content-Type: application/json');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_PUT, true);
// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get response from the server.
$resp = curl_exec($ch); 

echo '<br />Curl Response: ';
print_r($resp);

curl_close($ch);

 

I'm getting the curl response as:

Error in call to API function "files/search": request body: could not decode input as JSON

 Please tell me what correction needs to be done.

8 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    Looking over your code, there are a few things that should be fixed:

     

    - When specifying the root folder, you need to use the empty string "", not "/".

    - You'll need to POST (CURLOPT_POST) not PUT (CURLOPT_PUT).

    - You should not turn off SSL verification if possible.

    - The parameters must be passed as JSON in the HTTP request body, not in a header.

     

    You can find more information on using this endpoint here:

     

    https://www.dropbox.com/developers/documentation/http/documentation#files-search

     

    There's an example of calling an "RPC" endpoint like this in PHP here:

     

    https://stackoverflow.com/documentation/dropbox-api/412/listing-a-folder/1370/listing-the-root-folder-via-curl-in-php-and-the-curl-extension#t=20170615183729440977

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    That endpoint returns the file context in the response body. What your app does with that response is up to you. I.e., you can have it read it into memory and do something with it there, or you can save it to a local file.

    Here's an example that tells curl to save it to a local file:

    https://stackoverflow.com/documentation/dropbox-api/408/downloading-a-file/20965/downloading-a-file-with-metadata-via-curl-in-php#t=201706161828140145756

    Alternatively, instead of saving it to a local file using curl's CURLOPT_FILE option, you can read the file data from the curl_exec output, e.g., in that example saved as $output.
  • Priya M.'s avatar
    Priya M.
    Explorer | Level 4
    8 years ago

    Hi,

     

    Thank you for your reply.

     

    We do not want to save in local file. Also, reading in memory may cause memory exhausted issue with large files. Same is the issue with saving the read data in a variable when the file is large enough.

     

    Instead, we want to read the file in parts instead of reading the entire file at once. Is there any option to the same?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    The file data is returned in the HTTP response, so you could read from it as a stream if your HTTP client supports it.

    It looks like PHP curl's may effectively support this via CURLOPT_WRITEFUNCTION:

    http://docs.php.net/function.curl-setopt
  • Priya M.'s avatar
    Priya M.
    Explorer | Level 4
    8 years ago
    Hi,

    I didn't understand how the file data is returned as a stream. As far as I can see, it returns a normal read data. Can you please elaborate on this with an example?
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    Apologies, my reply was a little misleading. It seems CURLOPT_WRITEFUNCTION doesn't return a stream object exactly, but it's a way for you to supply a function that will be called multiple times to read data off the reply a piece at a time.

    In any case, this is more about using curl in PHP now, which is outside my area of expertise, so you may want to refer to the documentation or support community for PHP/curl for more information on using that.
  • Priya M.'s avatar
    Priya M.
    Explorer | Level 4
    8 years ago

    Thanks for your response.

     

    I will do the PHP part myself. I got confused by your incorrect reply previously and started searching more in that direction.

     

    Thanks again for your help.

About Dropbox API Support and Feedback

Node avatar for Dropbox API Support and Feedback
Get help with the Dropbox API from fellow developers and experts.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!