2017-01-02 102 views
0

我使用卷曲获得来自API调用下载的Excel文件,在我的API调用它返回一个Excel文件中的一个,卷曲API调用下载的文件

我喜欢强制下载软件,Excel文件,怎么能我用卷曲来做这个?

我试图像这样: -

$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); 
+0

这里有个答案http://stackoverflow.com/questions/22155882/php-curl-download-file – motanelu

+0

不,不像t帽子。我的API响应没有填写文件。不与数据的响应只包含下载文件。但insted下载文件。 @motanelu –

回答

1

如果您想保存您的服务器上的文件,添加

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

如果要强制下载到网站访问者,使用

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