检测一个网页上的链接是否都可以正常访问

部分源码来自:http://www.361way.com/php-curl-url/2779.html

<?php


//指定测试的地址(页面):

$viewUrl = "";
$host = "";


function _striplinks($document) {
	preg_match_all("'<\s*a\s.*?href\s*=\s*([\"\'])?(?(1) (.*?)\\1 | ([^\s\>]+))'isx", $document, $links);
	// catenate the non-empty matches from the conditional subpattern
	while (list($key, $val) = each($links[2])) {
		if (!empty($val))
			$match[] = $val;
	} while (list($key, $val) = each($links[3])) {
		if (!empty($val))
			$match[] = $val;
	}
	// return the links
	return $match;
}
/*===================================================================*\
	Function:	_expandlinks
	Purpose:	expand each link into a fully qualified URL
	Input:		$links			the links to qualify
				$URI			the full URI to get the base from
	Output:		$expandedLinks	the expanded links
\*===================================================================*/
function _expandlinks($links,$URI){
	$URI_PARTS = parse_url($URI);
	$host = $URI_PARTS["host"];
	preg_match("/^[^\?]+/",$URI,$match);
	$match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
	$match = preg_replace("|/$|","",$match);
	$match_part = parse_url($match);
	$match_root =
	$match_part["scheme"]."://".$match_part["host"];
	$search = array( 	"|^http://".preg_quote($host)."|i",
						"|^(\/)|i",
						"|^(?!http://)(?!mailto:)|i",
						"|/\./|",
						"|/[^\/]+/\.\./|"
					);
	$replace = array(	"",
						$match_root."/",
						$match."/",
						"/",
						"/"
					);
	$expandedLinks = preg_replace($search,$replace,$links);
	return $expandedLinks;
}

function _checkOk($url, $key){
	$headers = @get_headers($url);
	if ($headers[0] == 'HTTP/1.0 404 Not Found') {
		echo "\n".$key;
		echo '---<div style=" color: red; font-size: 18px; font-weight: bold;"> Not Found </div>';
		echo "\n";
	} else {
		echo "\n".$key;
		echo "---- ok \n";
	}
}




/*
 * 使用curl 的所有链接。
 */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 页面内容我们并不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回结果,而不是输出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
	echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);

// 主机部分,补全用
if (is_array($linkarr)) {
	foreach ($linkarr as $k => $v) {
		$linkresult[$k] = _expandlinks($v, $host);
	}
}

$linkresult = array_unique($linkresult);

echo '<pre>';
foreach ($linkresult as $key => $value){
	echo $key."--".$value;
	 _checkOk($value,$key);

}

echo "\n ==============RUN OVER=============== \n";
//printf("<p>此页面的所有链接为:</p><pre>%s</pre>\n", var_export($linkresult , true));


转载于:https://my.oschina.net/namenull/blog/214709

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值