You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

dghatch's avatar
dghatch
Helpful | Level 6
3 years ago

Download a file via API using PHP and cURL

Hi,

I am trying to download a file from my dropbox app, I have tried the cURL code in postman it works with no issues:

 

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

 

 

I have created the cURL as php and am running it on my server, but it doesn't work, I keep getting the "cat" returned to me.

 

 

 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://content.dropboxapi.com/2/files/download');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Authorization: Bearer ' . $token;
$headers[] = 'Dropbox-API-Arg: {"path":"' . $in_filepath . '"}';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
curl_close($ch);

var_dump ($result);

 

 

 

 

Any ideas what I have done wrong?

Thanks for your time.

  • I have now solved the issue, it turns out that the server (runing with DirectAdmin) was using an old version of curl (7.24.0) and when this was updated to 7.84.0 the problems was fixed.

     

    Thanks again for all your help.

  • kylea's avatar
    kylea
    Icon for Dropbox Staff rankDropbox Staff

    I tried a snippet nearly identical to yours on php 7.4.28 and it worked for me, though php seemed to like me setting the content type & accept headers as  well:

    $headers[] = "Content-Type: application/octet-stream";
    $headers[] = "Accept: application/octet-stream";

     

    I'm not 100% sure what the error is from just the image. I'd recommend trying to print out the response headers for additional information. The response code, along with the value of Dropbox-Api-Result in content endpoints can help diagnose.

     

    • dghatch's avatar
      dghatch
      Helpful | Level 6

      Thanks kylea 

       

      I tried using php 7.4 and 8, and I have printed the requestHeaders, but I am still getting the cat error:

      " array(11) { ["Host"]=> string(25) "www.host.com" ["Te"]=> string(8) "trailers" ["Sec-Fetch-User"]=> string(2) "?1" ["Sec-Fetch-Site"]=> string(4) "none" ["Sec-Fetch-Mode"]=> string(8) "navigate" ["Sec-Fetch-Dest"]=> string(8) "document" ["Upgrade-Insecure-Requests"]=> string(1) "1" ["Accept-Encoding"]=> string(17) "gzip, deflate, br" ["Accept-Language"]=> string(14) "en-GB,en;q=0.5" ["Accept"]=> string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" ["User-Agent"]=> string(80) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0" } 
      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        dghatch My colleague was asking about the response headers, but it looks like you've printed out the request headers here.

         

        In any case, I've also tried the code you shared here, plugging in my own access token and path, and as long as I fix the "Content-Type" as my colleague mentioned, it does work for me. (I am using PHP 7.4.29.)

         

        I have seen calls fail like this when the HTTP request itself has been malformed though, so make sure your request doesn't contain any invalid data. For example, make sure your $token and $in_filepath values don't contain any extra whitespace, etc.