0

I’m using curl to get downloaded Excel file from an api call, in one of my api call it returns a excel file,

I like to force download that Excel file , how can i do this with curl ?

I tries Like this :-

$url = 'http://xyzwebsite.com/GetExcelParameterScheet';
$fields = array('TemplateID' => $id);
$method = 'POST';

// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
    die('Curl failed: ' . curl_error($ch));
}

// Close connection
curl_close($ch);
keyz.Mist
  • 174
  • 2
  • 20
  • There's an answer here http://stackoverflow.com/questions/22155882/php-curl-download-file – motanelu Jan 02 '17 at 10:06
  • no, not like that. my API response not fill in file. response not with data only contain download file. but insted download file. @motanelu – keyz.Mist Jan 02 '17 at 10:18

1 Answers1

1

If you want to save the file on your server, add

file_put_contents("sheet.xls",$result);

If you want to force download to the website visitor, use

header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"sheet.xls\""); 
echo $result;
devondre
  • 493
  • 6
  • 14