mirror of
https://github.com/sigmasternchen/useful-api.org
synced 2025-03-15 07:58:55 +00:00
feat: Add domain Whois API
This commit is contained in:
parent
7708c7c0f1
commit
f5d3f75d2e
2 changed files with 60 additions and 3 deletions
|
@ -18,22 +18,41 @@ function getContactData(array $data) {
|
|||
$kind = array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "kind"))[0][3] ?? null;
|
||||
$email = array_map(fn(array $a) => $a[3], array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "email")));
|
||||
$adr = array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "adr"))[0]["label"] ?? null;
|
||||
$remarks = array_map(fn($r) => $r["title"], $entity["remarks"] ?? []);
|
||||
|
||||
$contacts[] = [
|
||||
"handle" => $entity["handle"],
|
||||
"handle" => $entity["handle"] ?? null,
|
||||
"roles" => $entity["roles"],
|
||||
"function" => $fn,
|
||||
"kind" => $kind,
|
||||
"address" => $adr,
|
||||
"email" => $email,
|
||||
"remarks" => $remarks,
|
||||
];
|
||||
}
|
||||
|
||||
return $contacts;
|
||||
}
|
||||
|
||||
function fetchError(array $response) {
|
||||
if ($response["status"] == 404) {
|
||||
setStatusCode(404);
|
||||
$description = "The requested object does not exist.";
|
||||
} else {
|
||||
setStatusCode(500);
|
||||
$description = "Remote error: " . $response["error"];
|
||||
}
|
||||
|
||||
return errorResponse("Unable to fetch results", $description);
|
||||
}
|
||||
|
||||
function whoisIp(string $ip) {
|
||||
$data = json_decode(getRequest(RDAP_URL . "/ip/" . $ip), true);
|
||||
$response = getRequest(RDAP_URL . "/ip/" . $ip);
|
||||
if ($response["isError"]) {
|
||||
return fetchError($response);
|
||||
}
|
||||
|
||||
$data = json_decode($response["body"], true);
|
||||
return [
|
||||
"query" => $ip,
|
||||
"version" => $data["ipVersion"],
|
||||
|
@ -50,6 +69,30 @@ function whoisIp(string $ip) {
|
|||
];
|
||||
}
|
||||
|
||||
function whoisDomain(string $domain) {
|
||||
$response = getRequest(RDAP_URL . "/domain/" . $domain);
|
||||
if ($response["isError"]) {
|
||||
return fetchError($response);
|
||||
}
|
||||
|
||||
$data = json_decode($response["body"], true);
|
||||
|
||||
return [
|
||||
"query" => $domain,
|
||||
"name" => $data["ldhName"],
|
||||
"unicodeName" => $data["unicodeName"] ?? $data["ldhName"],
|
||||
"nameservers" => array_map(fn($ns) => $ns["ldhName"], $data["nameservers"]),
|
||||
"registered" => array_values(
|
||||
array_filter(
|
||||
$data["events"],
|
||||
fn($e) => $e["eventAction"] == "registration"
|
||||
)
|
||||
)[0]["eventDate"] ?? null,
|
||||
|
||||
"contacts" => getContactData($data),
|
||||
];
|
||||
}
|
||||
|
||||
return function (array $context) {
|
||||
$result = null;
|
||||
|
||||
|
@ -61,6 +104,10 @@ return function (array $context) {
|
|||
} else {
|
||||
$result = whoisIp($ip);
|
||||
}
|
||||
} elseif (key_exists("domain", $_GET)) {
|
||||
$domain = $_GET["domain"];
|
||||
|
||||
$result = whoisDomain($domain);
|
||||
} else {
|
||||
setStatusCode(400);
|
||||
$result = errorResponse("Unknown mode", "Please specify one of the following query parameters: ip");
|
||||
|
|
|
@ -7,6 +7,10 @@ function request(string $method, string $url, string $body = null, array $header
|
|||
curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curlResource, CURLOPT_CUSTOMREQUEST, $method);
|
||||
curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($curlResource, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($curlResource, CURLOPT_FAILONERROR, true);
|
||||
|
||||
curl_setopt($curlResource, CURLOPT_VERBOSE, true);
|
||||
|
||||
if ($body)
|
||||
curl_setopt($curlResource, CURLOPT_POSTFIELDS, $body);
|
||||
|
@ -19,7 +23,13 @@ function request(string $method, string $url, string $body = null, array $header
|
|||
curl_setopt($curlResource, CURLOPT_HTTPHEADER, $headers);
|
||||
}
|
||||
|
||||
$result = curl_exec($curlResource);
|
||||
$body = curl_exec($curlResource);
|
||||
$result = [
|
||||
"body" => $body,
|
||||
"isError" => curl_errno($curlResource),
|
||||
"error" => curl_error($curlResource),
|
||||
"status" => curl_getinfo($curlResource, CURLINFO_RESPONSE_CODE),
|
||||
];
|
||||
|
||||
curl_close($curlResource);
|
||||
|
||||
|
|
Loading…
Reference in a new issue