$val){
$key===host && $val = $urlarr[host];
$key===referer && $val =http://.$urlarr[host];
$str .= $key:$val, ;
}
//虚拟来路.
!eregi(referer,$str) && $str .=referer:http://{$urlarr[host]}, ;
//经过修正, 基本上, 来路也是那个站, 主机也是url站点.
$header = array(trim($str));
//下面仅仅是选择用哪个程序来采集.
if($method === f&&function_exists(file_get_contents)) {
$opts = array(
http=>array(
method=>get,
header=>$header,
)
);
$cxcontext = stream_context_create($opts);
$file_contents = @file_get_contents($url, false, $cxcontext);
} elseif ($method === c&&function_exists(curl_init)) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,curlopt_httpheader,$header);
curl_setopt ($ch, curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer,1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
//为了让样式显示得漂亮,我们给它加一句目标引向.
$file_contents = str_replace(,,$file_contents);
//处理最常见的几种编码, 如果目标网站没有编码, 就默认为gbk
!preg_match(/charset=([^]*)/isu,$file_contents,$lang) && $lang[1]=gbk;
function_exists(mb_convert_encoding) && $file_contents = mb_convert_encoding($file_contents,empty($charset)?utf-8:$charset,$lang[1]);
//注销部分代码;
unset($url,$lang,$timeout,$urlarr,$charset);
return $file_contents;
}
//测试开始 测试用file_get_contents方式
header(content-type:text/html; charset=utf-8);
//http://www.xtzj.com/read-htm-tid-347550.html 这是采集不到.
$file = get_url_content(http://www.hao123.com,f);
$file = strip_tags($file,);
preg_match_all(/(http:[^]*)>/isu,$file,$link);unset($link[0]);
$link = $link[1];
//我们来模拟获得数据. 自己更换数字.0-151 下面是用curl方式
$x = 10;
$file = get_url_content($link[$x]);
echo $file;
?>
全部写上说明, 注释..
有不明白的回复.. 我来给采集普及一下知识.
原文地址:http://bbs.phpchina.com/viewthread.php?tid=99263
http://www.bkjia.com/phpjc/486604.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/486604.htmltecharticle?php function get_url_content($url,$method = c) { //引入需要的语言编码.如果没有, 就会默认为utf-8,不必担心. global $charset; $urlarr = parse_url($url); //如果检...