最新消息: 新版网站上线了!!!

php批量获取网址的标题关键字说明

header("Content-Type: text/html; charset=utf-8");
define('Root', str_replace("\\", '/', dirname(__FILE__)));
/**
 * 自动转字符串编码为UTF-8
 * @param  String $String 字符串
 * @return String
 */
function strCoding($String) 
{ 
	$encode = mb_detect_encoding($String, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
	if ($String != "UTF-8"){
		$String = iconv($encode,'UTF-8',$String);
	}
	return trim($String);
}
/**
 * 获取网址站点信息
 * @author 懒人程序 [i@kuitao8.com]
 * @param  String $Url 目标地址
 * @return Array
 */
function SiteInfo($Url){
	if(empty($Url)){return false;}
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $Url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	$contents=curl_exec($ch);
	curl_close($ch);
	$meta = get_meta_tags($Url);
	preg_match("%(.*?)<\/title.*?>%is",$contents,$title);//匹配代码中的标题部分
	$title['1'] = (isset($title['1']))?strCoding($title['1']):'';
	$description = (isset($meta['description']))?strCoding($meta['description']):'';
	 $keywords = (isset($meta['keywords']))?strCoding($meta['keywords']):'';
    $i = array('name'=>$title[1],'description'=>$description,'url'=>$Url,'keywords'=>$keywords);
	return $i;
}
/**
 * 数据记录
 * @author 懒人 程序 [i@kuitao8.com]
 * @param  Array  $SiteArr Url集数组
 * @return String
 */
function Record(array $SiteArr){
	$XmlUrl = null;
	if(count($SiteArr)<=0){die('请输入数据');}
	foreach ($SiteArr as $value) {
		$XmlUrl[] = SiteInfo($value);
	}
	$XmlUrl = serialize($XmlUrl);
	$logfile = fopen(Root.'/'.date('Y-m-d').'.txt',"w");
	fwrite($logfile, $XmlUrl);
	fclose($logfile);
	die('记录完成!');
}
$SiteArr = array('http://www.kuitao8.com','http://www.hao123.com');
echo Record($SiteArr);

转载请注明:谷谷点程序 » php批量获取网址的标题关键字说明