Files
QuickAPI/api/icp/index.php
muzihuaner d7177eaacc gx
2026-03-18 13:30:41 +08:00

294 lines
9.7 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
/**
* ICP备案查询API
* 支持通过域名查询网站的ICP备案信息
*/
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
// 访问计数
$counterFile = __DIR__ . '/counter.dat';
if (is_writable(dirname($counterFile))) {
$counter = (int)@file_get_contents($counterFile);
@file_put_contents($counterFile, $counter + 1, LOCK_EX);
}
// 获取请求参数
$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://api.vat.tools/query?domain=" . urlencode($domain);
$data = callApi($apiurl);
if ($data && is_array($data)) {
if (isset($data['icp']) && !empty($data['icp']) && $data['icp'] !== 'N/A') {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = [
'domain' => $domain,
'icp' => $data['icp'],
'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: 使用爱站网API
$apiurl2 = "https://tools.zhuaxia.com/icp?domain=" . urlencode($domain);
$data2 = callApi($apiurl2);
if ($data2 && is_array($data2) && isset($data2['data'])) {
$icpData = $data2['data'];
$icp = $icpData['icp'] ?? '';
if (!empty($icp) && $icp !== 'N/A') {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = [
'domain' => $domain,
'icp' => $icp,
'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;
}
}
// 方法3: 使用网格API (chaxun)
$apiurl3 = "https://www.chaxun.15600.net/service.php?domain=" . urlencode($domain);
$data3 = callApi($apiurl3);
if ($data3 && is_array($data3)) {
$icpNum = $data3['ICP'] ?? $data3['icp'] ?? '';
if (!empty($icpNum) && $icpNum !== 'N/A') {
$result['status'] = 'success';
$result['code'] = 200;
$result['message'] = '查询成功';
$result['data'] = [
'domain' => $domain,
'icp' => $icpNum,
'owner' => $data3['company'] ?? $data3['owner'] ?? 'N/A',
'siteName' => $data3['websitename'] ?? $data3['name'] ?? 'N/A',
'checkDate' => $data3['checkTime'] ?? $data3['time'] ?? 'N/A',
'source' => 'chaxun'
];
return $result;
}
}
// 方法4: 使用AllOrigins代理通过爱站网查询
$proxyUrl = "https://api.allorigins.win/get?url=" . urlencode("http://icp.aizhan.com/index.php?domain=" . urlencode($domain));
$data4 = callApi($proxyUrl);
if ($data4 && isset($data4['contents'])) {
$htmlData = $data4['contents'];
$parsedData = parseICPFromHTML($htmlData, $domain);
if ($parsedData && isset($parsedData['icp']) && $parsedData['icp'] !== 'N/A') {
return [
'status' => 'success',
'code' => 200,
'message' => '查询成功',
'domain' => $domain,
'data' => $parsedData
];
}
}
// 方法5: 直接查询爱站网 (HTTP)
$apiurl5 = "http://icp.aizhan.com/index.php?domain=" . urlencode($domain);
$htmlData = callApi($apiurl5, false);
if ($htmlData && strlen($htmlData) > 100) {
$parsedData = parseICPFromHTML($htmlData, $domain);
if ($parsedData && isset($parsedData['icp']) && $parsedData['icp'] !== 'N/A') {
return [
'status' => 'success',
'code' => 200,
'message' => '查询成功',
'domain' => $domain,
'data' => $parsedData
];
}
}
// 都失败返回未备案
$result['status'] = 'not_found';
$result['code'] = 404;
$result['message'] = '未找到ICP备案信息该域名可能未在中国备案';
} catch (Exception $e) {
$result['status'] = 'error';
$result['code'] = 500;
$result['message'] = 'API异常: ' . $e->getMessage();
}
}
/**
* 通用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, 8);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
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, 2);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$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) . '[^>]*>([^<]+)</',
'/<td[^>]*>' . preg_quote($label) . '<\/td>\s*<td[^>]*>([^<]+)<\/td>/',
'/<th[^>]*>' . preg_quote($label) . '<\/th>\s*<td[^>]*>([^<]+)<\/td>/',
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
$value = trim(strip_tags($matches[1]));
if (!empty($value)) {
return $value;
}
}
}
return 'N/A';
}
/**
* 从HTML解析ICP信息
*/
function parseICPFromHTML($html, $domain) {
// 清理HTML
$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 = '';
foreach ($icpPatterns as $pattern) {
if (preg_match($pattern, $html, $matches)) {
$icp = trim($matches[0]);
if (!empty($icp)) {
break;
}
}
}
// 如果没有找到ICP号返回null
if (empty($icp) || $icp === 'N/A') {
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'
];
}
?>