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); 
2
  • There's an answer here stackoverflow.com/questions/22155882/php-curl-download-file Commented Jan 2, 2017 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 Commented Jan 2, 2017 at 10:18

1 Answer 1

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; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.