51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

自动获取ICP备案号接口源码

每一个备案的网站在建立后都要在网站底部放上自己的ICP备案号,并链接到 ICP/IP地址/域名信息备案管理系统 官网。

而备案号很多人可能并不记得,所以每次都要先查询再填写,而且官网的查询特别拉跨,如果不想这么麻烦的话,可以根据下面的方法自己创建一个接口,然后实现自动依据当前网站的域名获取到备案号。

创建查询接口

新建一个php文件,命名为 icpapi.php,当然也可以随意命名,然后输入以下代码:

<?php
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET");
header("Access-Control-Allow-Headers:x-requested-with,content-type");
header("Content-Type:text/html,application/json; charset=utf-8");
if ($_GET['domain']) {
    $domain = getTopHost($_GET['domain']);
} else {
    exit("缺少参数");
}
$timeStamp = time();
$authKey = md5("testtest" . $timeStamp);
$token = json_decode(curl_post("auth", "authKey=$authKey&timeStamp=$timeStamp", "application/x-www-form-urlencoded;charset=UTF-8", "0"));
$token = $token->params->bussiness;
$query = json_decode(curl_post("icpAbbreviateInfo/queryByCondition", '{"pageNum":"","pageSize":"","unitName":"' . $domain . '"}', "application/json;charset=UTF-8", $token));
$query = json_encode($query->params->list);
$query = str_replace("[", "", $query);
$query = json_decode(str_replace("]", "", $query));
$icp = $query->serviceLicence;
$unitName = $query->unitName;
$natureName = $query->natureName;
if (!$token) {
    $icp = "服务器请求频率过高,请稍后再试";
    $msg = "查询失败";
    $code = "0";
} elseif (!$icp) {
    $icp = "未北岸";
    $msg = "查询成功";
    $code = "1";
} else {
    $msg = "查询成功";
    $code = "1";
}
$json = array(
    'icp' => $icp,
    'unitName' => $unitName,
    'natureName' => $natureName,
    'msg' => $msg,
    'result' => $code
);
print_r(json_encode($json, JSON_UNESCAPED_UNICODE));

function curl_post($url, $data, $Content, $token) {
    $ip = "101.".mt_rand(1,255).".".mt_rand(1,255).".".mt_rand(1,255);
    $ch = curl_init();
    $headers = array(
        "Content-Type: $Content",
        "Origin: https://beian.miit.gov.cn/",
        "Referer: https://beian.miit.gov.cn/",
        "token: $token",
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36",
        "CLIENT-IP: $ip",
        "X-FORWARDED-FOR: $ip"
    );
    curl_setopt($ch, CURLOPT_URL, "https://hlwicpfwc.miit.gov.cn/icpproject_query/api/" . $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
function getTopHost($url) {
    if (stristr($url, "http") === false) {
        $url = "http://" . $url;
    }
    $url = strtolower($url);
    $hosts = parse_url($url);
    $host = $hosts['host'];
    $data = explode('.', $host);
    $n = count($data);
    $preg = '/[\w].+\.(com|net|org|gov|edu)\.cn$/';
    $pregip = '/((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/';
    if (($n > 2) && preg_match($preg, $host)) {
        $host = $data[$n - 3] . '.' . $data[$n - 2] . '.' . $data[$n - 1];
    } elseif (preg_match($pregip, $host)) {
        $host = $host;
    } else {
        $host = $data[$n - 2] . '.' . $data[$n - 1];
    }
    return $host;
}

创建自动获取

继续新建一个php文件,命名为 icp.php,也可以自己命名,并输入以下代码:

<?php
header("Content-type: application/json; charset=utf-8");

// echo ok(get_Domain());
echo "{document.write('" . ok(get_Domain()) ."');}";
function ok($url){
    $url = '这里替换为你上面创建的接口地址?domain=' . $url;
    $data = curl_get($url);
    $data = json_decode($data, true);
    $info = $data['icp'];
    return $info ;
}

function get_Domain()
{
    $scriptpath = str_replace('\\', '/', $_SERVER['SCRIPT_NAME']);
    $sitepath = substr($scriptpath, 0, strrpos($scriptpath, '/'));
    $siteurl = ($_SERVER['SERVER_PORT'] == '443' ) . $_SERVER['HTTP_REFERER'] . $sitepath . '/';
    return $siteurl;
}

function curl_get($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn; R815T Build/JOP40D) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1');
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $content = curl_exec($ch);
    curl_close($ch);
    return ($content);
}

网站实现调用

此时在你需要调用显示的地方输入以下代码,即可实现ICP备案号的自动获取并显示。

<a href="http://beian.miit.gov.cn" target="_blank"><script src="这里填入自动获取地址"></script></a>

直接调用我的

如果你嫌麻烦不想去创建的话也可以直接使用我已经创建好的。

直接选择下面任一代码放到需要展示ICP备案号的地方即可。

无图标的调用代码:

<a href="http://beian.miit.gov.cn" target="_blank"><script src="https://www.zeyiwl.cn/api/icp.php"></script></a>

带图标的调用代码:

<img src="https://www.zeyiwl.cn/api/icp.png" style="height: 17px;"><script src="https://www.zeyiwl.cn/api/icp.php"></script>

这里使用的官方的接口,可能查询速度比较慢,如果你有其他的接口请自行替换。

赞(0)
未经允许不得转载:工具盒子 » 自动获取ICP备案号接口源码