Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

dghatch's avatar
dghatch
Helpful | Level 6
4 years ago
Solved

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.

7 Replies

  • kylea's avatar
    kylea
    Icon for Dropbox Staff rankDropbox Staff
    4 years ago

    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
    4 years ago

    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 Community Moderator rankDropbox Community Moderator
    4 years ago

    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.

  • dghatch's avatar
    dghatch
    Helpful | Level 6
    4 years ago

    Thanks Greg-DB and kylea 

     

    I think I have done it right this time (response headers this time):

     

     

        HTTP/1.1 400 Bad Request
        Content-Length: 14468
        Content-Type: text/html
        Vary: Accept-Encoding
        X-Dropbox-Response-Origin: local
        Date: Mon, 18 Jul 2022 17:11:48 GMT
        Server: envoy
        Connection: close
    
    string(14672) "HTTP/1.1 400 Bad Request Content-Length: 14468 Content-Type: text/html Vary: Accept-Encoding X-Dropbox-Response-Origin: local Date: Mon, 18 Jul 2022 17:11:48 GMT Server: envoy Connection: close 

     

     

    I'm starting to think it's something else, not code.   I have tried adding a random token and I get the same error.

     

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

    dghatch Thanks! It does seem like the request is getting malformed somehow. Have you inspected the exact values you're sending to https://content.dropboxapi.com/2/files/download? Make sure there's no extraneous data, in both the $token and $in_filepath values, as that could mangle the request, resulting in the generic error response.

  • dghatch's avatar
    dghatch
    Helpful | Level 6
    4 years ago

    Thanks for the help.

     

    I have run the php file locally (XAMP) and it runs with no issues at all, so something on the server seems to be causing the issue, so I am now investigating this and I will update when/if I have more information.

     

     

  • dghatch's avatar
    dghatch
    Helpful | Level 6
    4 years ago

    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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

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!