Php 从cURL获取内容类型
这对于获得头球非常有效Php 从cURL获取内容类型,php,curl,header,Php,Curl,Header,这对于获得头球非常有效 $imageurl = "http://p.twimg.com/AW2-vIUCAAE_1FN.png"; $ch = curl_init(); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_URL, $image_url); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt ($ch
$imageurl = "http://p.twimg.com/AW2-vIUCAAE_1FN.png";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$content = curl_exec ($ch);
curl_close ($ch);
print_r($content);
返回
HTTP/1.1 200 OK
Server: nginx
X-peep-host: den2twapi014
Last-Modified: Mon, 15 Aug 2011 03:41:50 GMT
ETag: "7283847-dbb8-4aa830b1dabf0"
Accept-Ranges: bytes
Content-Length: 56248
Cache-Control: public, max-age=21600
Expires: Wed, 28 Mar 2012 15:53:57 GMT
Content-Type: image/png
X-CDN: AKAM
Date: Wed, 28 Mar 2012 09:53:57 GMT
Connection: keep-alive
回显
内容类型的最佳方法是什么?在调用curl\u close()
之前,调用:
或者,如果您想要一个解析过的头部数组,这里有一个快速而肮脏的解决方案(警告:这对验证提供的字符串是否确实是HTTP响应没有任何作用,请谨慎使用):
试试这个:-
<?php
$image_url = "http://p.twimg.com/AW2-vIUCAAE_1FN.png";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$content = curl_exec ($ch);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
echo $contentType;
echo "<br/>";
curl_close ($ch);
echo "<pre>";
print_r($content);
?>
谢谢!你知道是否有一个有效的内容类型列表,主要是针对图像的,但最好都知道。
<?php
function parse_http_head ($str) {
$result = array();
// Split into lines
$lines = explode("\r\n", $str);
// Handle response line
$line = explode(' ', array_shift($lines), 3);
$version = explode('/', $line[0]);
$result['version'] = (float) $version[1];
$result['code'] = (int) $line[1];
$result['text'] = $line[2];
// Parse headers
$result['headers'] = array();
while ($line = trim(array_shift($lines))) {
list($name, $val) = explode(':', $line, 2);
$name = strtolower(trim($name)); // Header names are case-insensitive, so convert them all to lower case so we can easily use isset()
if (isset($result['headers'][$name])) { // Some headers (like Set-Cookie:) may appear more than once, so convert them to an array if necessary
$result['headers'][$name] = (array) $result['headers'][$name];
$result['headers'][$name][] = trim($val);
} else {
$result['headers'][$name] = trim($val);
}
}
return $result;
}
// $content comes from your code above
$parsed = parse_http_head($content);
print_r($parsed);
/*
Output:
Array
(
[version] => 1.1
[code] => 200
[text] => OK
[headers] => Array
(
[server] => nginx
[x-peep-host] => den2twapi014
[last-modified] => Mon, 15 Aug 2011 03:41:50 GMT
[etag] => "7283847-dbb8-4aa830b1dabf0"
[accept-ranges] => bytes
[content-length] => 56248
[cache-control] => public, max-age=21600
[expires] => Wed, 28 Mar 2012 15:53:57 GMT
[content-type] => image/png
[x-cdn] => AKAM
[date] => Wed, 28 Mar 2012 09:53:57 GMT
[connection] => keep-alive
)
)
*/
<?php
$image_url = "http://p.twimg.com/AW2-vIUCAAE_1FN.png";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$content = curl_exec ($ch);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
echo $contentType;
echo "<br/>";
curl_close ($ch);
echo "<pre>";
print_r($content);
?>