cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

Working with File Content Sent From API file download curl request (php code)

Working with File Content Sent From API file download curl request (php code)

FSUInnovation
Explorer | Level 4

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 3

Greg-DB
Dropbox Staff

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.

FSUInnovation
Explorer | 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-DB
Dropbox Staff

No, to download the file data from Dropbox you don't need to apply any special encoding.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    FSUInnovation Explorer | Level 4
What do Dropbox user levels mean?