通过PHP file_get_contents 方式写查询 ICP 备案 API(附源码)

先附上代码

<?php
header('content-type:application/json;charset=utf8');
$domain = $_GET['domain'];
$str = file_get_contents('http://icp.chinaz.com/'.$domain);
function GetBetween($str,$start,$end){
    $r = explode($start, $str);
    if (isset($r[1])){
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}
$unitname1 = GetBetween($str,'<span>主办单位名称</span>
                            <p>','</p>');
function GetBetween1($unitname1,$start,$end){
    $p = explode($start, $unitname1);
    if (isset($p[1])){
        $p = explode($end, $p[1]);
        return $p[0];
    }
    return '';
}
$unitname = GetBetween($unitname1,'">','</a>');
$nature = GetBetween($str,"<span>主办单位性质</span>
                            <p><strong class=\"fl fwnone\">","</strong></p>");
$icp = GetBetween($str,"<span>网站备案/许可证号</span>
                            <p><font>","</font>");
$sitename = GetBetween($str,'<span>网站名称</span>
                            <p>','</p>');
$url = GetBetween($str,'<span>网站首页网址</span>
                            <p class="Wzno">','</p>');
$check = GetBetween($str,'<span>审核时间</span>
                            <p>','</p>');
$result = [
    '主办单位名称' => $unitname,
    '主办单位性质' => $nature,
    '网站备案/许可证号' => $icp,
    '网站名称' => $sitename,
    '网站首页网址' => $url,
    '审核时间' => $check,
    ];
die(json_encode($result,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); //json返回
echo $result;
?>

一、找到对应信息及源代码

先去网站icp.chinaz.com查询信息

二、定义 PHP 函数 GetBetween(截取两字符之间的内容)

function GetBetween($str,$start,$end){
    $r = explode($start, $str);
    if (isset($r[1])){
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}

当然前一段代码也不能少呀(要有 $str 啦)。在这里

$url = $_GET['url']; //获取参数域名(url)
$str = file_get_contents('http://icp.chinaz.com/'.$url); //读取网页内容

三、截取想要的信息

$nature = GetBetween($str,"<span>主办单位性质</span>
                            <p><strong class=\"fl fwnone\">","</strong></p>");
$icp = GetBetween($str,"<span>网站备案/许可证号</span>
                            <p><font>","</font>");
$sitename = GetBetween($str,'<span>网站名称</span>
                            <p>','</p>');
$url = GetBetween($str,'<span>网站首页网址</span>
                            <p class="Wzno">','</p>');
$check = GetBetween($str,'<span>审核时间</span>
                            <p>','</p>');

问: 为啥没有截取主体单位名称?

答: 因为主体单位名称是一个 a 标签 且 href 值中含有与域名对应的变量(qq.com 这个对应的是深圳市腾讯计算机系统有限公司 )下面这样去除 href 的值或者 href 的值用 (.*?) 进行取值 尝试不成功! 在第 4 步解决这个问题

$unitname1 = GetBetween($str,'<span>主办单位名称</span>
                            <p>
                                <a target="_blank" href="">','</a>
                            </p>');

四、所以换了一个方法解决这个问题(先将 a 标签整体取出 在从 a 标签中取出想要的值(深圳市腾讯计算机系统有限公司))

function GetBetween($str,$start,$end){
    $r = explode($start, $str);
    if (isset($r[1])){
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}
$unitname1 = GetBetween($str,'<span>主办单位名称</span>
                            <p>','</p>'); //这里取出了整个a标签
function GetBetween1($unitname1,$start,$end){
    $p = explode($start, $unitname1);
    if (isset($p[1])){
        $p = explode($end, $p[1]);
        return $p[0];
    }
    return '';
}
$unitname = GetBetween($unitname1,'">','</a>'); //这里取出了想要的值

五、将所有取出的值放入数组

$result = [
    '主办单位名称' => $unitname,
    '主办单位性质' => $nature,
    '网站备案/许可证号' => $icp,
    '网站名称' => $sitename,
    '网站首页网址' => $url,
    '审核时间' => $check,
    ];

六、这一步是为了将第 7 步的返回 json 格式化显示

header('content-type:application/json;charset=utf8');

七、JSON返回

die(json_encode($result,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); //json返回
echo $result;

八、查询方式:

访问:
https://你的域名/xxx/index?url=baidu.com or https://你的域名/xxx?url=baidu.com (这种前提是楼上 php 代码的文件是默认文档)
示例:
https://api.xcboke.cn/api/icp?url=baidu.com
结果:

{
    "主办单位名称": "北京百度网讯科技有限公司",
    "主办单位性质": "企业",
    "网站备案/许可证号": "京ICP证030173号-1",
    "网站名称": "百度",
    "网站首页网址": "baidu.com",
    "审核时间": "2020-11-13"
}
© 版权声明
THE END
喜欢就支持一下吧
点赞1赞赏
分享
评论 抢沙发

请登录后发表评论