-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKisaWhois.php
More file actions
65 lines (55 loc) · 1.85 KB
/
KisaWhois.php
File metadata and controls
65 lines (55 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* 한국인터넷진흥원 KISA-WHOIS-OPEN-API : PHP-Client
* https://www.data.go.kr/data/15094277/openapi.do
* https://github.com/hompy-dev/KisaWhois
* @license https://github.com/hompy-dev/KisaWhois/blob/main/LICENSE (MIT License)
* @author <contact@hompy.dev>
*/
declare(strict_types=1);
namespace HompyDev\OpenApi;
class KisaWhois
{
// 인증키
private const SERVICE_KEY = 'YOUR_API_KEY'; // 유효한 인증키로 변경
// 서비스 URL
private const SERVICE_URL = 'https://apis.data.go.kr/B551505/whois';
// 응답형식 (json|xml)
private const ANSWER = 'json';
// 요청주소
private const ENDPOINTS = [
'domain' => self::SERVICE_URL . '/domain_name',
'ip' => self::SERVICE_URL . '/ip_address',
'asn' => self::SERVICE_URL . '/as_number',
'country' => self::SERVICE_URL . '/ipas_country_code'
];
public function lookup(string $query, string $mode = 'ip')
{
$params = [
'serviceKey' => self::SERVICE_KEY,
'query' => $query,
'answer' => self::ANSWER
];
$url = self::ENDPOINTS[$mode] . '?' . http_build_query($params);
return $this->request($url);
}
protected function request($url)
{
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response = curl_exec($ch);
if ($response === false) {
throw new \Exception('cURL request failed: ' . curl_error($ch));
}
curl_close($ch);
return $response;
}
catch (\Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
}