cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you interested in hearing how one of our Community members uses Dropbox for sailing trips? Read all about it here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Download a file via API using PHP and cURL

Download a file via API using PHP and cURL

dghatch
Helpful | Level 6
Go to solution

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

 

 

 

Screenshot 2022-07-15 153445.png

 

Any ideas what I have done wrong?

Thanks for your time.

1 Accepted Solution

Accepted Solutions

dghatch
Helpful | Level 6
Go to solution

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.

View solution in original post

7 Replies 7

kylea
Dropbox Staff
Go to solution

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
Helpful | Level 6
Go to solution

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
Dropbox Staff
Go to solution

@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
Helpful | Level 6
Go to solution

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
Dropbox Staff
Go to solution

@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
Helpful | Level 6
Go to solution

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
Helpful | Level 6
Go to solution

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.

Need more support?