curlはheader情報を取得する(php get_headersの代わりに)

8422 ワード

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が空の場合は配列が返され、そうでない場合は文字列が返されます.
以下はネットユーザーが共有する方法です.
一:[HP]タイムアウト機能付き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('//', $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はSOcketよりDNSをキャッシュできるという
$ch= curl_init($url);
//タイムアウト設定
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
//前の168文字を4枚のテスト図で読み取っても問題ないので、データが得られなければ適当に数値を大きくする
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);
3: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' ) );