php通过curl获取网页title内容

自媒体 投稿&转载 606浏览

前段时间研究了一段时间网站目录,标题其实也可以作为目录程序的重要数据,可惜很多网站编码等各种问题,导致无法简单的用函数就获得标题,网上看到一个相对较好的,虽然也不能说完美,但是可以记录下来,供以后参考学习

代码

/**$html = curl_get_file_contents($url);
$title = get_title_contents($html);
var_dump($title);*/
function curl_get_file_contents($url,$referer='') {
	static $curl_loops = 0;//避免死了循环必备
	static $curl_max_loops = 3;
	$useragent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";

	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_HEADER,true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
	curl_setopt($ch,CURLOPT_USERAGENT,$useragent);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($ch,CURLOPT_REFERER,$referer);
	$data = curl_exec($ch);
	$ret = $data;
	list($header,$data) = explode("

",$data,2);
	$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
	$last_url = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
	curl_close($ch);
	if ($http_code == 301 || $http_code == 302) {
		$matches = array();
		preg_match('/Location:(.*?)
/',$header,$matches);
		$url = @parse_url(trim(array_pop($matches)));
		if (!$url) {
			return $data;
		}
		$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (isset($url['query']) ? '?' . $url['query'] : '');
		if ($curl_loops++ >= $curl_max_loops) {
			return false;
		}else {
			$new_url = stripslashes($new_url);
			return curl_get_file_contents($new_url);
		}
	} else {
		list($header,$data) = explode("

",$ret,2);
		return $data;
	}
}
function get_title_contents($html){
	// 解析 HTML 的 <head> 区段
//	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
//	<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
	preg_match("/<head.*>(.*)</head>/smUi",$html, $htmlHeaders);
	//var_dump($output);die();
	if(!count($htmlHeaders)){
		$title = "无法解析数据中的 <head> 区段";
	}

// 取得 <head> 中 meta 设置的编码格式<meta charset="gb2312">
	if(preg_match('/<meta.*charset=(("){0,1}[a-zA-Z0-9-]*("){0,1})/',$htmlHeaders[1], $results)){
		$charset =  $results[1];
	}else{
		$charset = "None";
	}
	$charset = str_replace('"','',$charset);

// 取得 <title> 中的文字
	if(preg_match("/<title>(.*)</title>/Ui",$htmlHeaders[1], $htmlTitles)){
		if(!count($htmlTitles)){
			$title = "无法解析 <title> 的内容";
			exit;
		}

		// 将  <title> 的文字编码格式转成 UTF-8
		if($charset == "None"){
			$title=$htmlTitles[1];
		}else{
			$title=iconv($charset, "UTF-8", $htmlTitles[1]);
		}
	}
	return html_entity_decode($title);
}

以上代码支持301/302重定向,但我个人使用感觉还是存在问题,记录下来以后研究

推荐阅读

PHP提示:Warning: count():Parameter must be an array or an object that implements Countable

错误原因:PHP7.2以后,count()函数的参数无效时会抛出warning警告。...

PHP7.4 报错:Deprecated Functionality: implode(): Passing glue string after array is deprecated.

PHP7.4运行项目报错:Deprecated Functionality: implode(): Passing glue string after array is deprecated。只需交换 implode() 函数的两个参数!...

百度自动推送php实现代码

我们知道主动推送有助于搜索引擎抓取和收录网站内容,wordpress或者主流CMS其实已经集成了推送功能。但是如果我们要定期归还,而不是只在发布的时候推一次。这个功能可以通过结合PHP和Pagoda Panel的预定任务来实现。$api = '百度站......