【问题标题】:With php cURL get filename from file header使用 php cURL 从文件头获取文件名
【发布时间】:2010-12-17 12:41:28
【问题描述】:

我有这个 php cURL 函数:

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。我怎样才能做到这一点?

谢谢!

【问题讨论】:

    标签: php file curl filenames http-headers


    【解决方案1】:

    您需要分配一个回调来读取标题。

    curl_setopt($ch, CURLOPT_HEADERFUNCTION, readHeader);
    
    function readHeader($ch, $header)
    {
         // read headers
    }
    

    【讨论】:

    • 如其他答案中所述,除非您返回strlen( $header ),否则您对readheader 的调用将失败。
    【解决方案2】:

    我知道这个问题有点老了,但我想澄清一下CURLOPT_HEADERFUNCTION,我发现 php.net 上的文档令人困惑。

    curl 将一次向标头回调函数发送一个标头。回调函数必须返回读取的字节数,否则 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的返回,curl会在第一个header发送后结束。

    【讨论】:

    • 刚刚为我正在做的事情抓到了这个,curl_setopt 不应该在引号中包含 readHeader 吗?
    • 是的。谢谢乔希电子。我只是从我下面的答案中复制并附加了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多