function curl_get_headers($url, $opt = 0) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);
if($opt) {
$res = curl_getinfo($ch, $opt);
} else {
$res = curl_getinfo($ch);
}
curl_close($ch);
return $res;
}
$res = curl_get_headers($url, CURLINFO_FILETIME);
print_r($res);
opt参考http://www.php.net/manual/zh/function.curl-getinfo.php
如果$opt为空则返回数组,否则返回字符串。
以下是网友分享的方法:
一:[PHP]带超时功能的get_headers http://www.soulteary.com/2014/12/12/php-get_headers-with-timeout.html
function get_url_headers($url, $timeout = 10) {
$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, $timeout);
$data = curl_exec($ch);
$data = preg_split('/\n/', $data);
$data = array_filter(array_map(function($data) {
$data = trim($data);
if ($data) {
$data = preg_split('/:\s/', trim($data), 2);
$length = count($data);
switch ($length) {
case 2:
return array($data[0] => $data[1]);
break;
case 1:
return $data;
break;
default:
break;
}
}
}, $data));
sort($data);
foreach ($data as $key => $value) {
$value_keys = array_keys($value);
$itemKey = $value_keys[0];
if (is_int($itemKey)) {
$data[$key] = $value[$itemKey];
} elseif (is_string($itemKey)) {
$data[$itemKey] = $value[$itemKey];
unset($data[$key]);
}
}
return $data;
}
二:PHP高效获取远程图片尺寸和大小 http://www.open-open.com/lib/view/open1391692619473.html
/**
* 获取远程图片的宽高和体积大小
*
* @param string $url 远程图片的链接
* @param string $type 获取远程图片资源的方式, 默认为 curl 可选 fread
* @param boolean $isGetFilesize 是否获取远程图片的体积大小, 默认false不获取, 设置为 true 时 $type 将强制为 fread
* @return false|array
*/
functionmyGetImageSize($url,$type='curl',$isGetFilesize= false)
{
// 若需要获取图片体积大小则默认使用 fread 方式
$type=$isGetFilesize?'fread':$type;
if($type=='fread') {
// 或者使用 socket 二进制方式读取, 需要获取图片体积大小最好使用此方法
$handle=fopen($url,'rb');
if(!$handle)returnfalse;
// 只取头部固定长度168字节数据
$dataBlock=fread($handle, 168);
}
else{
// 据说 CURL 能缓存DNS 效率比 socket 高
$ch= curl_init($url);
// 超时设置
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
// 取前面 168 个字符 通过四张测试图读取宽高结果都没有问题,若获取不到数据可适当加大数值
curl_setopt($ch, CURLOPT_RANGE,'0-167');
// 跟踪301跳转
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// 返回结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$dataBlock= curl_exec($ch);
curl_close($ch);
if(!$dataBlock)returnfalse;
}
// 将读取的图片信息转化为图片路径并获取图片信息,经测试,这里的转化设置 jpeg 对获取png,gif的信息没有影响,无须分别设置
// 有些图片虽然可以在浏览器查看但实际已被损坏可能无法解析信息
$size=getimagesize('data://image/jpeg;base64,'.base64_encode($dataBlock));
if(empty($size)) {
returnfalse;
}
$result['width'] =$size[0];
$result['height'] =$size[1];
// 是否获取图片体积大小
if($isGetFilesize) {
// 获取文件数据流信息
$meta= stream_get_meta_data($handle);
// nginx 的信息保存在 headers 里,apache 则直接在 wrapper_data
$dataInfo= isset($meta['wrapper_data']['headers']) ?$meta['wrapper_data']['headers'] :$meta['wrapper_data'];
foreach($dataInfoas$va) {
if( preg_match('/length/iU',$va)) {
$ts=explode(':',$va);
$result['size'] = trim(array_pop($ts));
break;
}
}
}
if($type=='fread') fclose($handle);
return$result;
}
// 测试的图片链接
echo'
';
$result= myGetImageSize('http://s6.mogujie.cn/b7/bao/120630/2kpa6_kqywusdel5bfqrlwgfjeg5sckzsew_345x483.jpg_225x999.jpg','curl');
print_r($result);
三:php获取远程图片的大小宽度高度 http://www.kuitao8.com/20140509/2401.shtml
/** * Image class to simulate the getimagesize() function using the cURL and GD libraries * * @package img_info * @version 1.0.0 * @author MT Jordan* @copyright 2014
* @license zlib/libpng
* @link http://objsql.sourceforge.net
*/
class img_info
{
/**
* Determines how many characters will be read - set higher for larger files if getting errors
*/
private $range = 500000;
/**
* Constructor
*
* @access public
*/
public function __construct() {}
/**
* Method called to simulate the GD getimagesize() function w/o fopen wrapper
*
* @access public
* @param str $src
* @return mixed
*/
public function getimagesize( $src )
{
if ( function_exists( 'curl_version' ) )
{
$img_dim = $this->get_img_dim( $src );
$img_info = $this->get_img_info( $src );
return array( $img_dim[0],
$img_dim[1],
$img_info[0],
"width=\"$img_dim[0]\" height=\"$img_dim[1]\"",
'colors' => $img_dim[2],
'mime' => $img_info[1],
'size' => $img_info[2] );
}
else
{
trigger_error( 'The curl extension is not enabled.', E_USER_WARNING );
return false;
}
}
/**
* Private method returns dimensional info
*
* @access private
* @param str $src
* @return array
*/
private function get_img_dim( $src )
{
$headers = array( 'Range: bytes=0-' . $this->range );
$curl = curl_init( $src );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
$data = curl_exec( $curl );
curl_close( $curl );
$img = imagecreatefromstring( $data );
$colors = imagecolorstotal( $img );
$width = imagesx( $img );
$height = imagesy( $img );
$img_dim = array( $width, $height, $colors );
imagedestroy( $img );
return $img_dim;
}
/**
* Private method returns filetype and size info
*
* @access private
* @param str $src
* @return array
*/
private function get_img_info( $src )
{
$curl = curl_init( $src );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_URL, $src );
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_NOBODY, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
$data = str_replace( ' ', '', curl_exec( $curl ) );
curl_close( $curl );
$size = @explode( 'Content-Length:', $data );
//In the event Content-Length is not sent in the header
if ( array_key_exists( 1, $size ) )
{
$size = preg_split( '/\s+/', $size[1] );
$filesize = (int)$size[0];
}
else
$filesize = $this->get_img_size( $src );
$type = @explode( 'Content-Type:', $data );
$type = preg_split( '/\s+/', $type[1] );
if ( $type[0] === 'image/gif' )
$ext = 1;
elseif ( $type[0] === 'image/jpeg' || $type[0] === 'image/jpg' )
$ext = 2;
elseif ( $type[0] === 'image/png' )
$ext = 3;
else
$ext = 'N/A';
return array( $ext, $type[0], $filesize );
}
/**
* Private method returns filesize if Content-Length not sent in header
*
* @access private
* @param str $src
* @return int
*/
private function get_img_size( $src )
{
$curl = curl_init();
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $curl, CURLOPT_URL, $src );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_HEADER, false );
$data = curl_exec( $curl );
curl_close( $curl );
return strlen( $data );
}
}
$img = new img_info;
print_r( $img->getimagesize( 'http://upload.wikimedia.org/wikipedia/commons/0/04/Labrys-symbol-transparent.png' ) );
网友评论