We Want to Hear From You! What Do You Want to See on the Community? Tell us here!
Forum Discussion
FSUInnovation
6 years agoExplorer | Level 4
Working with File Content Sent From API file download curl request (php code)
I have a file that is in the form of a word doc on my dropbox application. I was able to get a working php curl without any errors from dropbox's side to download the file. I understand through the http docs that it is the file content that you are reading from the api. However, in my application, the curl is executed and there is no pop up from my browser to download the file. If I am using dropbox to download, how would I use the api request's content correctly for my app such that I could download the file content into a file on my machine which was the intended feature. Thus far, this what I have come up with from my code:
$timeout = 50; $url = "https://content.dropboxapi.com/2/sharing/get_shared_link_file"; $sql = $con -> query("SELECT * FROM intern WHERE InternName = '$pointer'"); $resume = $sql -> fetch(PDO::FETCH_ASSOC); $filename = str_replace('%', '_', explode('?', basename($resume['Resume']))[0]); $fp = fopen($filename , "w+"); $rcurl = curl_init(); $params = array( utf8_encode("Authorization: Bearer " . $resume['DropboxToken']), "Dropbox-API-Arg: " . json_encode(array( "url" => $resume['Resume'] ))); curl_setopt($rcurl, CURLOPT_URL, $url); curl_setopt($rcurl, CURLOPT_HTTPHEADER, $params); curl_setopt($rcurl, CURLOPT_FILE, $fp); curl_setopt($rcurl, CURLOPT_TIMEOUT, $timeout); curl_setopt($rcurl, CURLOPT_POST, false); curl_setopt($rcurl, CURLOPT_RETURNTRANSFER, true); $download = curl_exec($rcurl); $http_request = curl_getinfo($rcurl, CURLINFO_HTTP_CODE); echo $download; echo $http_request;
3 Replies
- Greg-DB6 years ago
Dropbox Community Moderator
In this code, you're telling curl (via the 'CURLOPT_FILE' option) to save the downloaded data to the "$fp" file pointer that you provided. That means that the file data should be getting saved to the location specified by "filename" on the local filesystem.
So, if you're building a web app where this code is running on your web server, the file is going to be downloaded to the web server's filesystem, and not automatically returned to the end-user's web browser.
You can serve the downloaded file or stream the data to the user's browser, but exactly how you do so will depend on your setup and web framework/library that you're using (if any). As that's not about using the Dropbox API though, I can't offer much help or specific details on how to do so. You may want to refer to the documentation for your programming language, web framework, and/or web server, etc. for information on how to do so.
- FSUInnovation6 years agoExplorer | Level 4
I guess the API related question I was is if I need to use any special encoding with that file data read from the API to prevent corruption regardless of how I choose to stream or use it.
- Greg-DB6 years ago
Dropbox Community Moderator
No, to download the file data from Dropbox you don't need to apply any special encoding.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,035 PostsLatest Activity: 5 months ago
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 or Facebook.
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!