前言
这个思路和写法也是大半年前的想法了,今天分享出来给大家的参考,更重要的还是希望有大佬能够指点完善一下,之前在网上也找过源码,可惜没找到。
思路以及源码
首先先创建个ICP备案查询接口,利用这个备案查询接口来完成后面的自动获取备案号并显示做铺垫,这个接口也是对接的官方接口,查询多了会有上限不过好在官方接口不会出现意外。
<?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;
}
接下来创建自动获取ICP备案号接口代码
<?php
header("Content-type: application/json; charset=utf-8");
// echo ok(get_Domain());
echo "{document.write('" . ok(get_Domain()) ."');}";
function ok($url){
$url = '这里文字替换成icp北岸查询接口地址?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);
}
这样我们的接口就创建好了,如何调用其实和正常添加备案号代码方法一样,只是把备案号替换成了接口地址而已
<a href="http://beian.miit.gov.cn" target="_blank"><script src="这里填入接口地址"></script></a>
总结
目前这个方法以及思路写法有个很大的缺点就是会高频占用服务器资源导致访问网站的时候会很卡,希望大佬可以指点并完善下代码,一起开源造福大家
看的我热血沸腾啊https://www.237fa.com/
看的我热血沸腾啊https://www.jiwenlaw.com/
赞,感谢分享
这个好,有源码
你好,看完你的博客文章,感觉很不错!希望与你网站首页友情链接
大流量卡
53go.cn
专注于移动/联通/电信推出的大流量多语音活动长短期套餐手机卡的相关知识的介绍普及
听说互换友情链接可以增加网站的收录量,特此来换,如果同意的话就给internetyewu@163.com发信息或者就在此回复下吧!
你写得非常清晰明了,让我很容易理解你的观点。
您好~我是腾讯云开发者社区运营,关注了您分享的技术文章,觉得内容很棒,我们诚挚邀请您加入腾讯云自媒体分享计划。完整福利和申请地址请见:https://cloud.tencent.com/developer/support-plan
作者申请此计划后将作者的文章进行搬迁同步到社区的专栏下,你只需要简单填写一下表单申请即可,我们会给作者提供包括流量、云服务器等,另外还有些周边礼物。
支持
无意冒犯 之前是找的这份代码吗[https://github.com/yitd/ICP-API](https://github.com/yitd/ICP-API)
打不开,可能GitHub又被墙了