2009-11-17 8 views
2

에서 파일 이름을 얻을 :내가이 PHP 컬 기능이 파일 헤더

function curl_login($url,$data,$proxy,$proxystatus){ 
$fp = fopen("cookietlt.txt", "w"); 
fclose($fp); 
$login = curl_init(); 
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt"); 
curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt"); 
curl_setopt($login, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
curl_setopt($login, CURLOPT_TIMEOUT, 40); 
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE); 
if ($proxystatus == 'on') { 
    curl_setopt($login, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($login, CURLOPT_HTTPPROXYTUNNEL, TRUE); 
    curl_setopt($login, CURLOPT_PROXY, $proxy); 
} 
curl_setopt($login, CURLOPT_URL, $url); 
curl_setopt($login, CURLOPT_HEADER, TRUE); 
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($login, CURLOPT_POST, TRUE); 
curl_setopt($login, CURLOPT_POSTFIELDS, $data); 
ob_start();  // prevent any output 
return curl_exec ($login); // execute the curl command 
ob_end_clean(); // stop preventing output 

curl_close ($login); 

unset($login);  
}     
function curl_grab_page($site,$proxy,$proxystatus){ 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
if ($proxystatus == 'on') { 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE); 
    curl_setopt($ch, CURLOPT_PROXY, $proxy); 
} 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); 
curl_setopt($ch, CURLOPT_URL, $site); 
ob_start();  // prevent any output 
return curl_exec ($ch); // execute the curl command 
ob_end_clean(); // stop preventing output 
curl_close ($ch); 
} 


curl_login('http://www.site.tdl/login.php','username=test&password=demo','','off'); 
curl_grab_page('http://www.site.tdl/1965.torrent','','off'); 

내가 변수로 파일 (http://www.site.tdl/1965.torrent) 헤더 파일 이름을 얻을 필요가있다.

http://www.site.tdl/1965.torrent 헤더 :

Content-Disposition: attachment; filename="Linux.Mint.torrent" 
Content-Type: application/x-bittorrent 
Content-Length: 4525 

그래서 출력은 Linux.Mint 될 것입니다. 어떻게해야합니까?

감사합니다.

답변

2

헤더를 읽으려면 콜백을 할당해야합니다.

curl_setopt($ch, CURLOPT_HEADERFUNCTION, readHeader); 

function readHeader($ch, $header) 
{ 
    // read headers 
} 
+0

다른 답변에서 언급했듯이'readheader'에 대한 호출은'strlen ($ header)'를 반환하지 않으면 실패합니다. – BryanH

13

내가이 질문은 옛날의 종류 알고,하지만 난 CURLOPT_HEADERFUNCTION 조금을 명확히하고 싶습니다, 나는 혼란에 php.net에 대한 문서를 발견했다.

curl은 헤더 콜백 함수를 한 번에 하나의 헤더로 보냅니다. 콜백 함수는 바이트 수를 읽거나 다른 컬이 실패합니다 (그리고 요청이 종료됩니다)

curl_setopt($ch, CURLOPT_HEADERFUNCTION, "readHeader"); 

function readHeader($ch, $header) 
{ 
    // read headers 
    echo "Read header: ", $header; 
    return strlen($header); 
} 

예 출력 반환해야합니다 : readHeader의 반환없이

Read header: HTTP/1.1 200 OK 
    Read header: Server: nginx/0.8.32 
    Read header: Date: Wed, 31 Mar 2010 14:23:18 GMT 
    Read header: Content-Type: image/jpeg 
    Read header: Content-Length: 886308 
    Read header: Connection: close 
    Read header: Accept-Ranges: bytes 
    Read header: 

을, 컬은 후 종료됩니다 첫 번째 헤더가 전송됩니다.

+2

curl_setopt에 따옴표로 된 readHeader가 있으면 안되는 문제입니다. – joshtronic

+0

참. 고마워요 조쉬 트로닉. 방금 복사하여 아래 답변에 추가했습니다. – deadkarma