php抓取图片curl,php curl抓取远程图片

function download_file($file_name, $url) {

$ch = curl_init($url);

$fp = fopen($file_name, "wb");

// set URL and other appropriate options

$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;';

$url_info = parse_url($url);

$host = $url_info['host'];

$header = array(

'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',

'Accept-Charset: UTF-8,*',

'Accept-Encoding: deflate',

'Accept-Language: en-us,en;q=0.5',

'Connection: keep-alive',

"Host: {$host}",

"User-Agent: {$user_agent}",

);

$options = array(CURLOPT_FILE => $fp,

/*CURLOPT_HEADER => 0,

CURLOPT_FOLLOWLOCATION => 1,

CURLOPT_REFERER => $url,

CURLOPT_TIMEOUT => 240

*/

CURLOPT_HEADER => 0,

CURLOPT_FOLLOWLOCATION => 1,

CURLOPT_TIMEOUT => 240,

CURLOPT_HTTPHEADER => $header,

); // 1 minute timeout (should be enough)

curl_setopt_array($ch, $options);

$ret = curl_exec($ch);

if (!$ret) {

var_export(curl_error($ch));

}

curl_close($ch); fclose($fp); return $ret;

}

function getinfo($uri) {

// start output buffering

ob_start();

// initialize curl with given uri

$ch = curl_init($uri);

$user_agent = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;’;

$url_info = parse_url($uri);

$host = $url_info[‘host’];

$header = array(

‘Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8′,

‘Accept-Charset: UTF-8,*’,

‘Accept-Encoding: gzip, deflate’,

‘Accept-Language: en-us,en;q=0.5′,

‘Connection: keep-alive’,

“Host: {$host}”,

“User-Agent: {$user_agent}”,

);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

// make sure we get the header

curl_setopt($ch, CURLOPT_HEADER, 1);

// make it a http HEAD request

curl_setopt($ch, CURLOPT_NOBODY, 1);

$okay = curl_exec($ch);

curl_close($ch);

// get the output buffer

$head = ob_get_contents();

// clean the output buffer and return to previous

// buffer settings

ob_end_clean();

// gets you the numeric value from the Content-Length

// field in the http header

var_export($head);exit;

$regex = ‘@Content-Length:\s(\d+)@’;

$count = preg_match($regex, $head, $matches);

// if there was a Content-Length field, its value

// will now be in $matches[1]

if(isset($matches[1])){

$size = $matches[1];

}else{

$size = 0;

}

$regex = ‘@Content-Type:\simage/(\w+)@’;

$count = preg_match($regex, $head, $matches);

// if there was a Content-Length field, its value

// will now be in $matches[1]

if(isset($matches[1])){

$type = $matches[1];

}else{

$type = ”;

}

//$last=round($size/(1024*1024),3);

//return $last.’ MB’;

return array(‘size’ => $size, ‘type’ => $type);

}

function getext($filename) {

return substr(strrchr($filename, ‘.’), 1);

}

$valid_file_type = array(

‘jpeg’ => ‘jpg’,

‘png’ => ‘png’,

‘gif’ => ‘gif’,

);

$url = ‘http://hiphotos.baidu.com/hackers365/pic/item/aa8b9a338d6f5518ac4b5fe6.jpg’;

/*

$file_info = getinfo($url);

var_export($file_info);

$filename = basename($url);

$ext = $valid_file_type[$file_info[‘type’]];

if ($ext != getext($filename)) {

$filename .= ‘.’ . $ext;

}

var_export($filename);*/

download_file(‘/tmp/hacker.jpg’, $url);

function download_file($file_name, $url) { $ch = curl_i […]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值