This commit is contained in:
muzihuaner
2026-03-18 13:23:58 +08:00
parent 9d84d7847a
commit 3c4af36e11

View File

@@ -84,26 +84,68 @@ function queryICP($domain) {
]; ];
try { try {
// 方法1: 使用国家互联网应急中心API // 方法1: 使用API Vat接口返回JSON格式
$apiurl = "https://icp.aizhan.com/index.php?domain=" . urlencode($domain); $apiurl = "https://api.vat.tools/query?domain=" . urlencode($domain);
$data = callApi($apiurl);
if ($data && isset($data['icp'])) {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = [
'domain' => $domain,
'icp' => $data['icp'] ?? 'N/A',
'owner' => $data['organizer'] ?? $data['owner'] ?? 'N/A',
'siteName' => $data['website_name'] ?? $data['name'] ?? 'N/A',
'checkDate' => $data['check_date'] ?? $data['approve_date'] ?? 'N/A',
'siteNature' => $data['site_nature'] ?? 'N/A',
'source' => 'api.vat.tools'
];
return $result;
}
$ch = curl_init(); // 方法2: 使用天聪API
curl_setopt($ch, CURLOPT_URL, $apiurl); $apiurl2 = "https://api.tiancong.info/api/queryIcp?domain=" . urlencode($domain);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data2 = callApi($apiurl2);
curl_setopt($ch, CURLOPT_TIMEOUT, 8); if ($data2 && isset($data2['code']) && $data2['code'] == 0 && isset($data2['data'])) {
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'); $icpData = $data2['data'];
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result['status'] = 'success';
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $result['code'] = 200;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $result['message'] = '查询成功';
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); $result['data'] = [
'domain' => $domain,
'icp' => $icpData['icp'] ?? 'N/A',
'owner' => $icpData['unitname'] ?? 'N/A',
'siteName' => $icpData['sitename'] ?? 'N/A',
'checkDate' => $icpData['checkdate'] ?? 'N/A',
'siteNature' => $icpData['sitenature'] ?? 'N/A',
'source' => 'tiancong.info'
];
return $result;
}
$response = curl_exec($ch); // 方法3: 使用免费ICP API
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $apiurl3 = "https://ijscode.top/api/icp.php?url=" . urlencode($domain);
curl_close($ch); $data3 = callApi($apiurl3);
if ($data3 && isset($data3['icp'])) {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = [
'domain' => $domain,
'icp' => $data3['icp'] ?? 'N/A',
'owner' => $data3['belong'] ?? 'N/A',
'siteName' => $data3['name'] ?? 'N/A',
'checkDate' => $data3['time'] ?? 'N/A',
'source' => 'ijscode'
];
return $result;
}
if ($httpCode === 200 && !empty($response)) { // 方法4: 使用aizhan.com (改进的HTML解析)
// 解析HTML获取ICP信息 $apiurl4 = "https://icp.aizhan.com/index.php?domain=" . urlencode($domain);
$parsedData = parseICPData($response, $domain); $htmlData = callApi($apiurl4, false);
if ($htmlData) {
$parsedData = parseICPData($htmlData, $domain);
if ($parsedData) { if ($parsedData) {
$result['status'] = 'success'; $result['status'] = 'success';
$result['code'] = 200; $result['code'] = 200;
@@ -113,22 +155,11 @@ function queryICP($domain) {
} }
} }
// 方法2: 备用API - ICP查询 // 方法5: 备用API - ICP查询
$backupUrl = "https://www.beianx.com/search/?q=" . urlencode($domain); $backupUrl = "https://www.beianx.com/search/?q=" . urlencode($domain);
$ch = curl_init(); $htmlResponse = callApi($backupUrl, false);
curl_setopt($ch, CURLOPT_URL, $backupUrl); if ($htmlResponse) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $backupData = parseBeianxData($htmlResponse, $domain);
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) { if ($backupData) {
$result['status'] = 'success'; $result['status'] = 'success';
$result['code'] = 200; $result['code'] = 200;
@@ -141,7 +172,7 @@ function queryICP($domain) {
// 如果都失败,返回默认的未备案状态 // 如果都失败,返回默认的未备案状态
$result['status'] = 'not_found'; $result['status'] = 'not_found';
$result['code'] = 404; $result['code'] = 404;
$result['message'] = '未找到ICP备案信息该域名可能未在中国备案'; $result['message'] = '未找到ICP备案信息该域名可能未在中国备案或各查询源暂时不可用';
} catch (Exception $e) { } catch (Exception $e) {
$result['message'] = '查询异常: ' . $e->getMessage(); $result['message'] = '查询异常: ' . $e->getMessage();
@@ -150,6 +181,41 @@ function queryICP($domain) {
return $result; return $result;
} }
/**
* 通用HTTP调用函数
*/
function callApi($url, $isJson = true) {
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/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, 3);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($httpCode !== 200 || empty($response)) {
return null;
}
if ($isJson) {
return json_decode($response, true);
}
return $response;
} catch (Exception $e) {
return null;
}
}
/** /**
* 解析aizhan.com返回的ICP数据 * 解析aizhan.com返回的ICP数据
*/ */
@@ -158,40 +224,58 @@ function parseICPData($html, $domain) {
$html = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/i', '', $html); $html = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/i', '', $html);
$html = preg_replace('/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/i', '', $html); $html = preg_replace('/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/i', '', $html);
// 提取ICP编号 // 提取ICP编号 - 多种格式支持
$icpPattern = '/[\d\u4e00-\u9fa5]+-ICP-\d{8}/iu'; $icpPatterns = [
$icpMatches = []; '/[\d\u4e00-\u9fa5]+-ICP-\d{8}/iu', // 原格式
if (preg_match_all($icpPattern, $html, $icpMatches)) { '/ICP[\u4e00-\u9fa5]?\d{8,}/iu', // 新格式
$icp = $icpMatches[0][0] ?? ''; '/[\d]{8}\|[\d]{8}/u', // 竖线格式
} else { ];
$icp = ''; $icp = '';
foreach ($icpPatterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
$icp = $matches[0];
break;
}
} }
// 提取主办单位 // 提取主办单位 - 多种选择器
$ownerPattern = '/主办单位.*?<td[^>]*>([^<]+)<\/td>/is'; $ownerPatterns = [
$ownerMatches = []; '/>([^<]*?单位[^<]*?)</i',
if (preg_match_all($ownerPattern, $html, $ownerMatches)) { '/主办单位[^>]*>([^<]+)</i',
$owner = trim($ownerMatches[1][0] ?? ''); '/<td[^>]*>([^<]*?单位[^<]*?)<\/td>/i',
} else { ];
$owner = ''; $owner = '';
foreach ($ownerPatterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
$owner = trim(strip_tags($matches[1]));
if (!empty($owner)) break;
}
} }
// 提取网站名称 // 提取网站名称
$sitePattern = '/网站名称.*?<td[^>]*>([^<]+)<\/td>/is'; $siteNamePatterns = [
$siteMatches = []; '/网站名称[^>]*>([^<]+)</i',
if (preg_match_all($sitePattern, $html, $siteMatches)) { '/<td[^>]*>网站名称<\/td>\s*<td[^>]*>([^<]+)</i',
$siteName = trim($siteMatches[1][0] ?? ''); ];
} else {
$siteName = ''; $siteName = '';
foreach ($siteNamePatterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
$siteName = trim(strip_tags($matches[1]));
if (!empty($siteName)) break;
}
} }
// 提取审核时间 // 提取审核时间
$datePattern = '/审核时间.*?<td[^>]*>([^<]+)<\/td>/is'; $datePatterns = [
$dateMatches = []; '/审核时间[^>]*>([^<]+)</i',
if (preg_match_all($datePattern, $html, $dateMatches)) { '/<td[^>]*>([12]\d{3}-\d{2}-\d{2})<\/td>/i',
$checkDate = trim($dateMatches[1][0] ?? ''); ];
} else {
$checkDate = ''; $checkDate = '';
foreach ($datePatterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
$checkDate = trim(strip_tags($matches[1]));
if (!empty($checkDate)) break;
}
} }
if (!empty($icp) || !empty($owner) || !empty($siteName)) { if (!empty($icp) || !empty($owner) || !empty($siteName)) {
@@ -215,16 +299,35 @@ function parseBeianxData($html, $domain) {
$html = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/i', '', $html); $html = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/i', '', $html);
$html = preg_replace('/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/i', '', $html); $html = preg_replace('/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/i', '', $html);
// 提取ICP编号 // 提取ICP编号 - 多种格式
$icpPattern = '/[\d\u4e00-\u9fa5]+-ICP-\d{8}/iu'; $icpPatterns = [
$icpMatches = []; '/[\d\u4e00-\u9fa5]+-ICP-\d{8}/iu',
preg_match_all($icpPattern, $html, $icpMatches); '/ICP[\u4e00-\u9fa5]?\d{8,}/iu',
$icp = $icpMatches[0][0] ?? ''; '/>([^<]*ICP[^<]*?)\</i',
];
$icp = '';
foreach ($icpPatterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
$icp = trim(isset($matches[1]) ? $matches[1] : $matches[0]);
if (!empty($icp)) break;
}
}
// 从表格中提取信息 // 从表格中提取信息
$owner = extractTableValue($html, '主办单位'); $owner = extractTableValue($html, '主办单位');
if (empty($owner)) {
$owner = extractTableValue($html, '单位');
}
$siteName = extractTableValue($html, '网站名称'); $siteName = extractTableValue($html, '网站名称');
if (empty($siteName)) {
$siteName = extractTableValue($html, '名称');
}
$checkDate = extractTableValue($html, '审核时间'); $checkDate = extractTableValue($html, '审核时间');
if (empty($checkDate)) {
$checkDate = extractTableValue($html, '时间');
}
if (!empty($icp) || !empty($owner) || !empty($siteName)) { if (!empty($icp) || !empty($owner) || !empty($siteName)) {
return [ return [