Php CURL get header返回与get_headers不同
我想从文件中获取标题。 但是CURL返回值与get_头不同 不知道为什么。 结果如下: 使用get_标题Php CURL get header返回与get_headers不同,php,curl,header,content-type,Php,Curl,Header,Content Type,我想从文件中获取标题。 但是CURL返回值与get_头不同 不知道为什么。 结果如下: 使用get_标题 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,
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] =>
)
这是我的get header by 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_setopt($ch,CURLOPT_HTTPGET,true);
但是它也会返回文件内容(body),如何仅使用CURL而不使用文件内容来获取确切的文件内容类型?get\u头接收图像的信息,而另一个不是。我想你可以查一下网址。因为两者必须相同。很容易获取内容类型或其他信息:
<?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);
@Baba嗨,我的PHP版本是5.3。是否可以提供图像URL进行测试???即使在PHP 5.3Seen上也无法复制您的问题。。我已经看到了这个问题。。。如果设置curl\u setopt($ch,CURLOPT\u NOBODY,false)代码>它也可以工作。。。让我看得更深入一些。我认为问题在于get_头发送get请求,而CURL with CURLOPT_NOBODY将请求方法设置为HEAD。如果更改CURLOPT_HTTPGET=true,一切都会正常:(已经看到…破坏了一些测试,可以肯定…如果我设置CURL_setopt($ch,CURLOPT_HTTPGET,true);,则使用CURL,但它也会返回文件内容?!好的,我编辑了代码以处理您的案例,重点是CUSTOMREQUEST选项。