2017-03-17 126 views
0

當我訪問一個鏈接,如:https://demo.com/report?transType=xls它將給我下載返回XLS。它的響應頭:如何閱讀內容處置XLS文件返回:附件與PHP

HTTP/1.1 200 OK 
Date: Thu, 16 Mar 2017 19:18:37 GMT 
Server: IIS/4.0 (Windows XP) 
Content-Disposition: attachment; filename*="utf-8''demo.xls 
Vary: Accept-Encoding,User-Agent 
Content-Length: 14848 
Keep-Alive: timeout=10, max=1000 
Connection: Keep-Alive 

的Content-Type:text/html的

我怎麼能這個文件下載到我的服務器使用PHP(捲曲,插座...)?我嘗試了CURL,但它不起作用。保存的文件無法讀取:(這是我的代碼:

$op = curl_init(); 
curl_setopt ($op, CURLOPT_URL, 'https://demo.com/report?transType=xls'); 
curl_setopt ($op, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($op, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($op, CURLOPT_TIMEOUT, 60); 
curl_setopt ($op, CURLOPT_POST, 1); 
curl_setopt ($op, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt ($op, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt ($op, CURLOPT_BINARYTRANSFER, 1); 

$response = curl_exec($op); 
@file_put_contents('saved.xls', $response); 

請幫我:(

回答

0

下載使用curl在PHP中,你必須使用代碼是像這樣一個文件:

<?php 
//File to save the contents to 
$fp = fopen ('theFile.xls', 'w+'); 
$url = "https://demo.com/report?transType=xls"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
//give curl the file pointer so that it can write to it 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$data = curl_exec($ch);//get curl response 
//done 
curl_close($ch); 
fclose($fp); 
?> 
+0

非常感謝你:) – AskToBeAsked