1

我正在尝试通过 curl 下载文件,它的处理过程没有错误,但只返回“内部服务器错误。请联系系统管理员。” 通过浏览器,在网站上,当您单击链接时,它会让您留在页面上并开始下载文件。如果您复制链接位置并将其放入浏览器,它会返回相同的文本 - “内部服务器错误。请联系系统管理员。”

经过几天的研究,我迷路了

我的功能:

public function requestFile($url,$filename){
    $cookie_file_path = "cookiejar.txt"; 
    if(!file_exists($cookie_file_path)) {
        $fh = fopen($cookie_file_path, "w");
        fwrite($fh, "");
        fclose($fh);
    }
    $fp_header = '';
    $agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
    if ( ! ($fp_header = tmpfile())) { die('File not created'); }
    set_time_limit(0);
    $fp = fopen ($filename, 'w+');//This is the file where we save the    information
    $ch = curl_init(str_replace(" ","%20",$url));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 500);
    curl_setopt($ch, CURLOPT_WRITEHEADER, $fp_header);
    curl_setopt($ch, CURLINFO_CONTENT_TYPE,"text/csv");
    curl_setopt($ch, CURLOPT_FILE, $fp); // here it sais to curl to just save it
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,TRUE);
    $data = curl_exec($ch);//get curl response
    print_r(curl_getinfo($ch));
    curl_close($ch);
    fclose($fp);

    echo "Headers ===>\n";
    rewind($fp_header); echo stream_get_contents($fp_header);
    echo "<=== Headers\n";
    fclose($fp_header);     
}

我留下了一些调试代码,可能还有一些我尝试过的额外内容:

curl_getinfo:

Array
(
[url] => <hidden>/download_price.php?f=Pricelist.csv
[content_type] => text/html; charset=ISO-8859-1
[http_code] => 200
[header_size] => 478
[request_size] => 267
[filetime] => -1
[ssl_verify_result] => 19
[redirect_count] => 0
[total_time] => 0.249
[namelookup_time] => 0
[connect_time] => 0.015
[pretransfer_time] => 0.202
[size_upload] => 0
[size_download] => 59
[speed_download] => 236
[speed_upload] => 0
[download_content_length] => 59
[upload_content_length] => 0
[starttransfer_time] => 0.249
[redirect_time] => 0
[certinfo] => Array
    (
    )

[redirect_url] => 
)

和标题

Headers ===>
HTTP/1.0 200 OK
Date: Sat, 16 Mar 2013 11:25:29 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Set-Cookie: cookie_test=please_accept_for_session; expires=Mon, 15-Apr-2013 11:25:29     GMT; path=/; domain=<hidden>
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: User-Agent,Accept-Encoding
Content-Length: 59
Connection: close
Content-Type: text/html; charset=ISO-8859-1
<=== Headers

我猜我只是缺少某种类型的请求等。任何提示、技巧、解决方案或其他调试建议都会很棒。

谢谢约翰

来自浏览器的跟踪:

Request URL:https://<hidden>/download_price.php?f=Pricelist.csv
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:sID=80149acc19e0e2383fa7124e026758a7; cookie_test=please_accept_for_session;  gallery-fadeshow1=3 
Host:<hiden>
Referer:https://<hidden>/account.php
User-Agent:Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22
Query String Parametersview sourceview URL encoded
f:Pricelist.csv
Response Headersview source
Cache-Control:public
Connection:Keep-Alive
Content-Description:File Transfer
Content-Disposition:attachment; filename="Pricelist.csv"
Content-Length:1799461
Content-Transfer-Encoding:binary
Content-Type:application/vnd.ms-excel
Date:Sat, 16 Mar 2013 12:15:02 GMT
Expires:0
Keep-Alive:timeout=5, max=100
Pragma:public
Server:Apache
Set-Cookie:cookie_test=please_accept_for_session; expires=Mon, 15-Apr-2013 12:15:02 GMT; path=/; domain=<hidden>
Vary:User-Agent
X-Powered-By:PHP/5.2.17
4

0 回答 0