This commit is contained in:
muzihuaner
2026-03-18 13:20:29 +08:00
parent a5a679b69b
commit e397c951bc

View File

@@ -1,5 +1,5 @@
<?php <?php
$file = 'counter.dat'; $file = 'counter.dat';
// 读取并自增 // 读取并自增
$counter = (int)@file_get_contents($file); $counter = (int)@file_get_contents($file);
@@ -7,104 +7,247 @@ $counter++;
// 使用 LOCK_EX 防止并发写入冲突 // 使用 LOCK_EX 防止并发写入冲突
file_put_contents($file, $counter, LOCK_EX); file_put_contents($file, $counter, LOCK_EX);
?> ?>
<?php <?php
Header('Content-type: Application/json; charset=utf-8'); /**
require '../../need.php'; * ICP备案查询API
$Request = need::Request(); * 支持通过域名查询网站的ICP备案信息
$type = isset($Request['type']) ? $Request['type'] : 'json'; //获取返回格式 默认json */
$url = isset($Request['url']) ? $Request['url'] : '';
new icp(['url'=>$url, 'type'=>$type]); header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
class icp // 获取请求参数
{ $domain = isset($_GET['domain']) ? trim($_GET['domain']) : '';
private $info = []; $type = isset($_GET['type']) ? trim($_GET['type']) : 'json';
private $array = [];
private $Message; // 验证域名参数
public function __construct(array $array) if (empty($domain)) {
{ http_response_code(400);
/* php构造函数 类初始化时自动运行 */ echo json_encode([
foreach($array as $k=>$v) 'status' => 'error',
{ 'code' => 400,
/* for循环获取参数并赋值到类中使用 */ 'message' => '缺少必要参数: domain',
$this->info[$k] = $v; 'example' => '/api/icp/index.php?domain=example.com'
} ], JSON_UNESCAPED_UNICODE);
$this->parameterException(); //调用类中方法 exit;
}
private function parameterException()
{
/* 判断参数是否正常 */
if(!stristr($this->info['url'], 'http'))
{
$this->info['url'] = 'http://'.$this->info['url'];
}
$host = parse_url($this->info['url'], PHP_URL_HOST);
if(!$host)
{
$this->evaluation(array('code'=>-1, 'text'=>'请输入正确的域名'));
return;
}
$this->run(); //调用方法
return true;
}
private function Run()
{
$scheme = @parse_url($this->info['url'], PHP_URL_SCHEME);
$host = @parse_url($this->info['url'], PHP_URL_HOST);
$url = $scheme.'://'.$host;
$data = json_decode(trim(preg_replace('/([a-z]+):/i', '"$1":', preg_replace('/jQuery\((.*)\)/', '$1', need::teacher_curl('https://micp.chinaz.com/Handle/AjaxHandler.ashx?action=GetPermit&callback=jQuery&query='.$host.'&type=host&_=1672673081420')))));
// print_r($data);
if(!isset($data->Typ))
{
$this->evaluation(array('code'=>-2, 'text'=>'备案数据获取失败'));
return;
}
$String = '';
$array = array(
'unit'=>$data->ComName,
'property'=>$data->Typ,
'record'=>$data->Permit,
'hostname'=>$data->WebName,
);
$String = "主办单位:{$data->ComName}\n单位性质{$data->Typ}\n网站名字{$data->WebName}\n网站备案号{$data->Permit}";
$String = trim($String, PHP_EOL);
$this->evaluation(array('code'=>1, 'text'=>'获取成功', 'data'=>$array), $String);
return;
}
private function delete() : bool
{
/* 删除已有参数 */
unset($this->array, $this->Message);
return true;
}
private function evaluation(array $array, $string = false) : bool
{
/* 输出数据 */
$string = $string ? $string : $array['text']; //判断$string是否存在
$this->delete(); //调用删除方法
$this->array = $array; //赋值
$this->Message = $string; //赋值
$this->result(); //输出内容
return true;
}
private function result()
{
$info = $this->info;
$type = isset($info['type']) ? $info['type'] : 'json';
Switch($type)
{
case 'text':
need::send($this->Message, 'text');
break;
default:
need::send($this->array, 'json');
break;
}
return true;
}
} }
// 清理域名格式移除http://或https://
$domain = preg_replace('/^https?:\/\//i', '', $domain);
$domain = preg_replace('/\/.*$/', '', $domain);
$domain = strtolower(trim($domain));
// 验证域名格式
if (!preg_match('/^([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$/i', $domain)) {
http_response_code(400);
echo json_encode([
'status' => 'error',
'code' => 400,
'message' => '无效的域名格式',
'domain' => $domain
], JSON_UNESCAPED_UNICODE);
exit;
}
// 查询ICP信息
$result = queryICP($domain);
// 根据type参数返回不同格式
if ($type === 'text') {
header('Content-Type: text/plain; charset=utf-8');
if ($result['status'] === 'success') {
echo "域名: " . $result['data']['domain'] . "\n";
echo "主办单位: " . $result['data']['owner'] . "\n";
echo "ICP编号: " . $result['data']['icp'] . "\n";
echo "审核时间: " . $result['data']['checkDate'] . "\n";
echo "网站名称: " . $result['data']['siteName'] . "\n";
} else {
echo "查询失败: " . $result['message'];
}
} else {
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
/**
* 查询ICP信息
* @param string $domain 域名
* @return array 返回结果数组
*/
function queryICP($domain) {
$result = [
'status' => 'error',
'code' => 500,
'message' => '查询失败',
'domain' => $domain,
'data' => null
];
try {
// 方法1: 使用国家互联网应急中心API
$apiurl = "https://icp.aizhan.com/index.php?domain=" . urlencode($domain);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200 && !empty($response)) {
// 解析HTML获取ICP信息
$parsedData = parseICPData($response, $domain);
if ($parsedData) {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = $parsedData;
return $result;
}
}
// 方法2: 备用API - ICP查询
$backupUrl = "https://www.beianx.com/search/?q=" . urlencode($domain);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $backupUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$backupResponse = curl_exec($ch);
$backupHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($backupHttpCode === 200 && !empty($backupResponse)) {
$backupData = parseBeianxData($backupResponse, $domain);
if ($backupData) {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = $backupData;
return $result;
}
}
// 如果都失败,返回默认的未备案状态
$result['status'] = 'not_found';
$result['code'] = 404;
$result['message'] = '未找到ICP备案信息该域名可能未在中国备案';
} catch (Exception $e) {
$result['message'] = '查询异常: ' . $e->getMessage();
}
return $result;
}
/**
* 解析aizhan.com返回的ICP数据
*/
function parseICPData($html, $domain) {
// 移除脚本和样式
$html = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/i', '', $html);
$html = preg_replace('/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/i', '', $html);
// 提取ICP编号
$icpPattern = '/[\d\u4e00-\u9fa5]+-ICP-\d{8}/iu';
$icpMatches = [];
if (preg_match_all($icpPattern, $html, $icpMatches)) {
$icp = $icpMatches[0][0] ?? '';
} else {
$icp = '';
}
// 提取主办单位
$ownerPattern = '/主办单位.*?<td[^>]*>([^<]+)<\/td>/is';
$ownerMatches = [];
if (preg_match_all($ownerPattern, $html, $ownerMatches)) {
$owner = trim($ownerMatches[1][0] ?? '');
} else {
$owner = '';
}
// 提取网站名称
$sitePattern = '/网站名称.*?<td[^>]*>([^<]+)<\/td>/is';
$siteMatches = [];
if (preg_match_all($sitePattern, $html, $siteMatches)) {
$siteName = trim($siteMatches[1][0] ?? '');
} else {
$siteName = '';
}
// 提取审核时间
$datePattern = '/审核时间.*?<td[^>]*>([^<]+)<\/td>/is';
$dateMatches = [];
if (preg_match_all($datePattern, $html, $dateMatches)) {
$checkDate = trim($dateMatches[1][0] ?? '');
} else {
$checkDate = '';
}
if (!empty($icp) || !empty($owner) || !empty($siteName)) {
return [
'domain' => $domain,
'icp' => $icp ?: 'N/A',
'owner' => $owner ?: 'N/A',
'siteName' => $siteName ?: 'N/A',
'checkDate' => $checkDate ?: 'N/A',
'source' => 'aizhan.com'
];
}
return null;
}
/**
* 解析beianx.com返回的数据
*/
function parseBeianxData($html, $domain) {
$html = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/i', '', $html);
$html = preg_replace('/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/i', '', $html);
// 提取ICP编号
$icpPattern = '/[\d\u4e00-\u9fa5]+-ICP-\d{8}/iu';
$icpMatches = [];
preg_match_all($icpPattern, $html, $icpMatches);
$icp = $icpMatches[0][0] ?? '';
// 从表格中提取信息
$owner = extractTableValue($html, '主办单位');
$siteName = extractTableValue($html, '网站名称');
$checkDate = extractTableValue($html, '审核时间');
if (!empty($icp) || !empty($owner) || !empty($siteName)) {
return [
'domain' => $domain,
'icp' => $icp ?: 'N/A',
'owner' => $owner ?: 'N/A',
'siteName' => $siteName ?: 'N/A',
'checkDate' => $checkDate ?: 'N/A',
'source' => 'beianx.com'
];
}
return null;
}
/**
* 从HTML表格中提取指定标签的值
*/
function extractTableValue($html, $label) {
$pattern = '/' . preg_quote($label) . '[^<]*?<[^>]*>([^<]+)<[^>]*>/is';
if (preg_match($pattern, $html, $matches)) {
return trim($matches[1]);
}
return '';
}