使用 php cURL 从文件头获取文件名

发布于 2024-08-11 16:36:51 字数 2322 浏览 2 评论 0原文

我有这个 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 header:

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

因此输出将为 Linux.Mint。我怎样才能做到这一点?

谢谢!

I have this php cURL function:

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

I need to get filename from file(http://www.site.tdl/1965.torrent) header into variable.

http://www.site.tdl/1965.torrent header:

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

So output will be Linux.Mint. How can I do that?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-08-18 16:36:51

我知道这个问题有点老了,但我想稍微澄清一下 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将在发送第一个标头后结束。

I know this question is kind of old, but I'd like to clarify CURLOPT_HEADERFUNCTION a bit, I found the documentation on php.net to be confusing.

curl will send the header callback function one header at a time. The call back function must return the number of bytes read or else curl will fail (and the request will end)

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

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

Example output:

 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: 

Without the return from readHeader, curl will end after the first header is sent.

一身骄傲 2024-08-18 16:36:51

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

curl_setopt($ch, CURLOPT_HEADERFUNCTION, readHeader);

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

You need to assign a callback to read the headers.

curl_setopt($ch, CURLOPT_HEADERFUNCTION, readHeader);

function readHeader($ch, $header)
{
     // read headers
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文