Files
QuickAPI/api/icp/index.php
muzihuaner 56fbaf08f0 fix
2026-03-18 13:27:52 +08:00

337 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
$file = 'counter.dat';
// 读取并自增
$counter = (int)@file_get_contents($file);
$counter++;
// 使用 LOCK_EX 防止并发写入冲突
file_put_contents($file, $counter, LOCK_EX);
?>
<?php
/**
* ICP备案查询API
* 支持通过域名查询网站的ICP备案信息
*/
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
// 获取请求参数
$domain = isset($_GET['domain']) ? trim($_GET['domain']) : '';
$type = isset($_GET['type']) ? trim($_GET['type']) : 'json';
// 验证域名参数
if (empty($domain)) {
http_response_code(400);
echo json_encode([
'status' => 'error',
'code' => 400,
'message' => '缺少必要参数: domain',
'example' => '/api/icp/index.php?domain=example.com'
], JSON_UNESCAPED_UNICODE);
exit;
}
// 清理域名格式移除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://www.api.vat.tools/query?domain=" . urlencode($domain);
$data = callApi($apiurl);
if ($data && is_array($data) && !empty($data)) {
if (isset($data['icp']) && $data['icp'] != 'N/A') {
$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' => 'vat.tools'
];
return $result;
}
}
// 方法2: 使用ICP查询-接口-通用API
$apiurl2 = "https://api.search.naver.com/search.naver?query=" . urlencode($domain) . "&where=web";
// 方法2b: 使用工信部接口
$apiurl2b = "https://beian.miit.gov.cn/";
// 方法3: 使用免费的ICP API - API1
$apiurl3 = "https://api.allorigins.win/get?url=" . urlencode("https://icp.aizhan.com/index.php?domain=" . urlencode($domain));
$data3 = callApi($apiurl3);
if ($data3 && isset($data3['contents'])) {
$htmlData = $data3['contents'];
$parsedData = parseICPFromHTML($htmlData, $domain);
if ($parsedData && (!isset($parsedData['icp']) || $parsedData['icp'] !== 'N/A')) {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = $parsedData;
return $result;
}
}
// 方法4: 使用爱站网缓存结果
$apiurl4 = "https://tools.zhuaxia.com/icp?domain=" . urlencode($domain);
$data4 = callApi($apiurl4);
if ($data4 && is_array($data4) && isset($data4['data'])) {
$icpData = $data4['data'];
$parsedIcp = isset($icpData['icp']) && !empty($icpData['icp']) ? $icpData['icp'] : 'N/A';
if ($parsedIcp !== 'N/A') {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = [
'domain' => $domain,
'icp' => $parsedIcp,
'owner' => $icpData['unitname'] ?? $icpData['owner'] ?? 'N/A',
'siteName' => $icpData['sitename'] ?? $icpData['name'] ?? 'N/A',
'checkDate' => $icpData['checkdate'] ?? $icpData['time'] ?? 'N/A',
'source' => 'zhuaxia'
];
return $result;
}
}
// 方法5: 使用网格API
$apiurl5 = "https://www.chaxun.15600.net/service.php?domain=" . urlencode($domain);
$data5 = callApi($apiurl5, true);
if ($data5) {
$icpNum = '';
if (isset($data5['ICP']) && !empty($data5['ICP'])) {
$icpNum = $data5['ICP'];
} elseif (isset($data5['icp']) && !empty($data5['icp'])) {
$icpNum = $data5['icp'];
}
if (!empty($icpNum)) {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = [
'domain' => $domain,
'icp' => $icpNum,
'owner' => $data5['company'] ?? $data5['owner'] ?? 'N/A',
'siteName' => $data5['websitename'] ?? $data5['name'] ?? 'N/A',
'checkDate' => $data5['checkTime'] ?? 'N/A',
'source' => 'chaxun'
];
return $result;
}
}
// 方法6: 爱站网直接查询
$apiurl6 = "http://icp.aizhan.com/index.php?domain=" . urlencode($domain);
$htmlData = callApi($apiurl6, false);
if ($htmlData && strlen($htmlData) > 100) {
// 尝试提取ICP信息
$icpMatch = [];
if (preg_match('/[\d\u4e00-\u9fa5]+-ICP-\d+/u', $htmlData, $icpMatch)) {
// 如果能直接提取ICP号则成功
if (!empty($icpMatch[0]) && strpos($icpMatch[0], 'ICP') !== false) {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = [
'domain' => $domain,
'icp' => $icpMatch[0],
'owner' => extractFromHTML($htmlData, '主办单位'),
'siteName' => extractFromHTML($htmlData, '网站名称'),
'checkDate' => extractFromHTML($htmlData, '审核时间'),
'source' => 'aizhan-cache'
];
return $result;
}
}
}
// 如果都失败,返回默认的未备案状态
$result['status'] = 'not_found';
$result['code'] = 404;
$result['message'] = '未找到ICP备案信息该域名可能未在中国备案或查询源暂时不可用';
} catch (Exception $e) {
$result['message'] = '查询异常: ' . $e->getMessage();
}
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);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
'Accept-Encoding: gzip, deflate',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200 || empty($response)) {
return null;
}
if ($isJson) {
return json_decode($response, true);
}
return $response;
} catch (Exception $e) {
return null;
}
}
/**
* 从HTML中提取指定字段的值
*/
function extractFromHTML($html, $label) {
$patterns = [
'/' . preg_quote($label) . '[^>]*>([^<]+)<',
'/<th[^>]*>' . preg_quote($label) . '<\/th>\s*<td[^>]*>([^<]+)<\/td>/',
'/<td[^>]*>' . preg_quote($label) . '<\/td>\s*<td[^>]*>([^<]+)<\/td>/',
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
return trim(strip_tags($matches[1]));
}
}
return 'N/A';
}
/**
* 改进的ICP HTML解析函数
*/
function parseICPFromHTML($html, $domain) {
$html = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/i', '', $html);
$html = preg_replace('/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/i', '', $html);
// 提取ICP编号
$icpPatterns = [
'/[\d\u4e00-\u9fa5]+-ICP-\d{8,}/u',
'/ICP[\u4e00-\u9fa5]?\d{8,}/u',
'/ICP\d{8,}/u',
];
$icp = 'N/A';
foreach ($icpPatterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
$icp = trim($matches[0]);
break;
}
}
// 如果没有找到有效的ICP返回null
if ($icp === 'N/A' || empty($icp)) {
return null;
}
$owner = extractFromHTML($html, '主办单位');
$siteName = extractFromHTML($html, '网站名称');
$checkDate = extractFromHTML($html, '审核时间');
return [
'domain' => $domain,
'icp' => $icp,
'owner' => $owner,
'siteName' => $siteName,
'checkDate' => $checkDate,
'source' => 'html-parse'
];
}
/**
* 解析aizhan.com返回的ICP数据
*/
function parseICPData($html, $domain) {
return parseICPFromHTML($html, $domain);
}
/**
* 解析beianx.com返回的数据
*/
function parseBeianxData($html, $domain) {
return parseICPFromHTML($html, $domain);
}
/**
* 从HTML表格中提取指定标签的值
*/
function extractTableValue($html, $label) {
return extractFromHTML($html, $label);
}