2009-11-17 71 views
2

获取文件名我有这个PHP卷曲功能:用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

正如其他答案中所提到的,除非返回'strlen($ header)',否则对'readheader'的调用将失败。 – BryanH 2012-09-26 16:13:45

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); 
} 

输出示例:

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: 

在不脱离readHeader返回,卷曲会之后结束第一个标题被发送。

+2

只是为我正在处理的东西阻塞了这个,不应该curl_setopt有引号中的readHeader吗? – joshtronic 2010-08-23 18:42:03

+0

是的。谢谢joshtronic。我只是复制并附上了我的答案 – deadkarma 2010-08-23 19:54:24