Could anyone help me diagnose an issue I am experiencing when downloading files from the Box content endpoint using PHP?
I'm able to upload content fine and I receive the correct response of RAW data when using the download endpoint. The trouble starts there however..
In the bolded portion when I attempt to output the raw data to the user for download, I end up with a corrupted file when trying to open the file. Does anyone have some tips for forcing the download of this raw data correctly to the web browser? I'm using PHP in this example, but if there is a better approach, I am open to investigating.
I am pulling the file name and other info from the "Get File's Info" endpoint, but have omitted it to remove extra stuff from my code example.
$url = "https://api.box.com/2.0/files/** file id **/content?access_token=".$token;
$header = '';
$header = array();
//////////////////////////////////////////
// Set Required CURL options
//////////////////////////////////////////
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "test", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10);
//////////////////////////////////////////
// Specify API endpoint
//////////////////////////////////////////
$ch = curl_init ($endpoint);
//////////////////////////////////////////
// Write options
//////////////////////////////////////////
curl_setopt_array ( $ch, $options );
//////////////////////////////////////////
// Execute
//////////////////////////////////////////
$headerh'response'] = curl_exec ( $ch );
$headerh'errno'] = curl_errno ( $ch );
$headerh'errmsg'] = curl_error ( $ch );
$headerh'httpcode'] = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
//////////////////////////////////////////
// Close connection /unbind
//////////////////////////////////////////
curl_close ($ch);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=** some file name **');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
print($headerh'response']);