通过抓取站长工具的过期查询来实现,下面是实现代码:
<?php
header('Content-type: application/json');
$domain = isset($_REQUEST['domain']) ? $_REQUEST['domain'] : '';
if ($domain == '') {
exit(json_encode(['code' => 201, 'msg' => '缺少domain参数'], JSON_UNESCAPED_UNICODE));
}
if (!preg_match('/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/', $domain)) {
exit(json_encode(['code' => 202, 'msg' => '域名格式有误'], JSON_UNESCAPED_UNICODE));
}
$data = domain('http://whois.chinaz.com/' . $domain);
preg_match('/创建时间<\/div><div class=\"fr WhLeList-right\"><span>(.*?)<\/span><\/div><\/li>/', $data, $reg);
preg_match('/过期时间<\/div><div class=\"fr WhLeList-right\"><span>(.*?)<\/span>/', $data, $over);
$reg = $reg[1];
$over = $over[1];
if ($reg == '' || $over == '') {
exit(json_encode(array('code' => 203, 'msg' => '查询失败', 'domain' => $domain), JSON_UNESCAPED_UNICODE));
} else {
exit(json_encode(array('code' => 200, 'msg' => '查询成功', 'domain' => $domain, 'reg' => $reg, 'over' => $over), JSON_UNESCAPED_UNICODE));
}
function domain($url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpheader[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
$httpheader[] = "Accept-Encoding: gzip, deflate, sdch, br";
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($header) {
curl_setopt($ch, CURLOPT_HEADER, true);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($referer) {
if ($referer == 1) {
curl_setopt($ch, CURLOPT_REFERER, 'http://whois.chinaz.com/');
} else {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
}
if ($ua) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0");
}
if ($nobaody) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
评论