<?php
function check_url($url){
$url_pieces = parse_url($url);//解析url,返回数组
//设置路径和端口
$path = (isset($url_pieces['path']))?$url_pieces['path']:'/';
$port = (isset($url_pieces['port']))?$url_pieces['port']:80;
if (!!$fp = fsockopen($url_pieces['host'],$port,$errno,$errstr,30)){//连接
//向服务器发送一些数据
$send = "HEAD $path HTTP/1.1\r\n"; // \r\n是为了满足正确的请求格式
$send .= "HOST:{$url_pieces['host']}\r\n";
$send .= "CONNECT:Close\r\n\r\n";
fwrite($fp, $send);
//读应答 $data = fgets($fp, 128);
fclose($fp);//关闭连接
list($reponse, $code) = explode(' ', $data);
if ($code == 200) {
return array($code,'good');
}else{
return array($code,'bad');
}
}else{
return array($errstr,'bad');
}
}
$urls = array(
'http://www.larryullman.com/',
'http://www.larryullman.com/wp-admin/',
'http://www.larryullman.com/tutorials'
);
echo '<h2>Validating URLs</h2>';
set_time_limit(0);
foreach ($urls as $url){
list($code ,$class) = check_url($url);
echo "<p><a href=\"$url\" taget=\"new\">$url</a>(<span class=\"$class\">$code</span>)</p>\n";
}
?>