首页 > php开发, 技术学习 > 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 […]

  1. 还没有评论
评论提交中, 请稍候...

留言

(Spamcheck Enabled)

Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks