<?php
//curl请求
function http_request($url,$data= ''){
$ch = curl_init();
if($data){
curl_setopt($ch,CURLOPT_POSTFIELDST,$data);
}
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,6);//设置一个长整形数,作为最大延续多少秒。
curl_setopt($ch,CURLOPT_HEADER,1);//响应头信息
$output = curl_exec($ch);
$info = curl_getinfo($ch);// 获取一个curl连接资源句柄的信息
if($output === FALSE){
$info = ['errcode'=>curl_errno($ch),'errmsg'=>curl_error($ch)];
}
curl_close($ch);//关闭连接
//$res['header'] = $output;
//$res['output'] = explode("\r\n",$output);
//$res['info'] = $info;
return $info;
}
//检查请求的头部信息
function checkHeader($url=''){
if(!$url){
return false;
}
$url = trim($url);//去除2边空白字符
if(strpos(strtolower($url),'http') === false){
$url = 'http://'.$url;
}
$res = http_request($url);
//$res = 1;
return $res;
}
//echo exec('curl -o /dev/null -s -w %{http_code} "wwww.ylhskxz.cn"');
if($_SERVER['REQUEST_METHOD']=='GET') {
echo <<<EOT
<div style="width:100%;margin:auto;">
<form action="testIcp.php" method="POST">
<textarea style="width:100%;height:300px;overflow: scroll;margin-bottom: 20px;;" name="domain"></textarea>
<input style="display:block;padding:5px;margin-left:10px;cursor: pointer;" type="submit" value="submit">
</form>
</div>
EOT;
return;
}else {
$t1 = microtime(true);
$d = trim($_POST['domain']);
$arr = explode("\n", $d);
//print_r($arr);
$rs = [];
$rs2 = [];
$dropes = [];
$normal = [];
$error = [];
foreach ($arr as $k => $v) {
if (empty(trim($v))) {
continue;
}
//$url = $v;
$v = trim($v);
$url = "http://{$v}/favicon.ico?t=" . microtime(true);
$res = checkHeader($url);
if(!isset($res['errcode'])) {
$code = $res['http_code'];
if ($code != 200) {
$dropes[] = ['domain' => $v, 'http_code' => $code, 'is_icp' => 0];
} else {
$normal[] = ['domain' => $v, 'http_code' => $code, 'is_icp' => 1];
}
}else{
$error[] = ['domain' => $v, 'http_code' => $res['errmsg'], 'is_icp' => 0];
}
}
$rs['drops'] = $dropes;
$rs['normal'] = $normal;
$rs['error'] = $error;
ksort($rs);
$t2 = microtime(true);
echo '脚本执行时间'.number_format((float)$t2-(float)$t1,3).'s'.',内存消耗:'.round(memory_get_usage()/(1024*1024),3).'M';
foreach ($rs as $k => $drops) {
echo '<link href="http://zlkm-persist.oss-cn-shanghai.aliyuncs.com/static/css/base-table.css" rel="stylesheet" type="text/css"><link rel="stylesheet" href="//image.tianhu.me/static/css/font-awesome-47.css"><style>td{height:40px;} a{color:#3c8dbc;} .fa-check{color:green;}</style>';
echo '<div class="list mT10" style="margin:50px auto;font-size:1.5rem;"><table width="100%" border="0" cellpadding="0" cellspacing="0">';
echo "<caption><h1>{$k}统计</h1></caption>";
echo '<tr class="tit" >';
echo '<th>编号</th><th>域名</th><th>http_code</th><th>备案状态</th>';
echo '</tr>';
//使用双层for语句嵌套二维数组$contact1,以HTML表格的形式输出
//使用外层循环遍历数组$contact1中的行
if (count($drops) == 0) {
echo '<tr class="td-json tr-bg red"><td colspan="13">暂无数据</td></tr>';
} else {
for ($row = 0; $row < count($drops); $row++) {
echo $drops[$row]['http_code'] !=200 ? '<tr class="td-json tr-bg red">': '<tr class="td-json tr-bg">';
echo '<td>' . ($row + 1) . '</td>';
//使用内层循环遍历数组$contact1 中 子数组的每个元素,使用count()函数控制循环次数
echo '<td>' . $drops[$row]['domain'] . '</td>';
echo '<td>' . $drops[$row]['http_code'] . '</td>';
if($drops[$row]['is_icp'] ) {
echo '<td><a href="javascript:;"><i class="fa fa-btn fa-check"></i></a></td>';
}else{
echo '<td><a href="javascript:;"><i class="fa fa-btn fa-times"></i></a></td>';
}
echo '</tr>';
}
}
echo '</table></div>';
}
效果