使用get_headers,CURL get header返回不同

时间:2012-11-16 08:03:21

标签: php curl header content-type

我想从文件中获取标题。 但CURL返回值与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
)

和CURL no Content-Type~>多数民众赞成我想要的

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] => 
)

这是我用curl函数获取标题

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 with return Content-Type if if set

curl_setopt($ch,CURLOPT_HTTPGET,true);

但是它还会返回文件内容(正文),如何只使用CURL获取文件内容类型而没有文件内容?

2 个答案:

答案 0 :(得分:1)

get_headers正在接收图像的信息,但另一个则没有。猜猜你可以查看网址。因为两者都必须相同。

答案 1 :(得分:1)

很容易获得CONTENT_TYPE或其他cURL信息:

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

输出:

  

string(10)“image / jpeg”