2012-11-16 47 views
1

我想从文件中获取标头。 但CURL返回值与get_headers不同。CURL get header返回与get_headers不同

不知道为什么。 这里的结果:

与get_headers

Array 
(
    [0] => HTTP/1.0 200 OK 
    [1] => Content-Type: image/jpeg 
    [2] => Last-Modified: Wed, 14 Nov 2012 11:06:07 GMT 
    [3] => X-Cache-Status: HIT 
    [4] => Expires: Tue, 19 Jan 2038 03:14:07 GMT 
    [5] => Cache-Control: max-age=2592000 
    [6] => X-Cache: HIT 
    [7] => Content-Length: 120185 
    [8] => Connection: close 
    [9] => Date: Fri, 16 Nov 2012 08:01:15 GMT 
    [10] => Server: lighttpd/1.4.26 
) 

和卷曲没有内容类型〜>这就是我想要什么

Array 
(
    [0] => HTTP/1.1 200 OK 
    [1] => Content-Length: 120185 
    [2] => Connection: close 
    [3] => Date: Fri, 16 Nov 2012 08:01:42 GMT 
    [4] => Server: lighttpd/1.4.26 
    [5] => 
    [6] => 
) 

这里是由卷曲功能我GET头

function get_headers_curl($url) 
{ 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL,   $url); 
    curl_setopt($ch, CURLOPT_HEADER,   true); 
    curl_setopt($ch, CURLOPT_NOBODY,   true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT,  15); 

    $r = curl_exec($ch); 
    $r = split("\n", $r); 
    return $r; 
} 

CURL与返回Content-Type如果我设置

curl_setopt($ch,CURLOPT_HTTPGET,true); 

但它会返回文件内容(正文),如何获得完全文件内容类型只有CURL和没有文件内容?

+0

什么PHP的版本? – Baba

+0

@Baba嗨,我的PHP版本是5.3 – TomSawyer

+0

是否有可能提供用于测试的图像URL?不能很好地复制你的问题,甚至PHP 5.3的 – Baba

回答

1

get_headers正在接收图像的信息,但另一个不是。猜你可以检查网址。因为两者必须是相同的。

+0

CURL与工作,如果我设置curl_setopt($ ch,CURLOPT_HTTPGET,真),但它会返回文件内容呢? – TomSawyer

1

很容易得到CONTENT_TYPE或其他卷曲资料:

<?php 
// Create a curl handle 
$ch = curl_init('link_to_file'); 

curl_setopt($ch, CURLOPT_NOBODY, TRUE); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE); 

// Execute 
curl_exec($ch); 

// Check if any error occured 
if (!curl_errno($ch)) { 
    var_dump(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); 
} 

// Close handle 
curl_close($ch); 

输出:

串(10) “图像/ JPEG”

+0

好吧,我编辑代码与您的案例一起工作,重点在CUSTOMREQUEST选项。 – Tuanitim

+0

您接受答案吗? – Tuanitim