├── .phpunit.result.cache ├── src ├── IllegalDomainException.php ├── WhoisServiceProvider.php ├── Whois.php └── WhoisQuery.php ├── README.md └── composer.json /.phpunit.result.cache: -------------------------------------------------------------------------------- 1 | {"version":1,"defects":[],"times":{"Larva\\Whois\\Tests\\WhoisQueryTest::testParseDomain":0.343,"Larva\\Whois\\Tests\\WhoisQueryTest::testLookupRaw":1.604,"Larva\\Whois\\Tests\\WhoisQueryTest::testLookupInfo":1.045}} -------------------------------------------------------------------------------- /src/IllegalDomainException.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class IllegalDomainException extends \Exception 15 | { 16 | 17 | } -------------------------------------------------------------------------------- /src/WhoisServiceProvider.php: -------------------------------------------------------------------------------- 1 | 17 | */ 18 | class WhoisServiceProvider extends ServiceProvider 19 | { 20 | /** 21 | * Register the service provider. 22 | * 23 | * @return void 24 | */ 25 | public function register(): void 26 | { 27 | $this->app->singleton(WhoisQuery::class, function () { 28 | return new WhoisQuery(); 29 | }); 30 | } 31 | } -------------------------------------------------------------------------------- /src/Whois.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class Whois extends Facade 24 | { 25 | /** 26 | * Get the registered name of the component. 27 | * 28 | * @return string 29 | */ 30 | protected static function getFacadeAccessor(): string 31 | { 32 | return WhoisQuery::class; 33 | } 34 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-whois 2 | 3 |

4 | 5 | Stable Version 6 | Total Downloads 7 | License 8 |

9 | 10 | Laravel 的 Whois 查询模块。 11 | 12 | 13 | ## 环境需求 14 | 15 | - PHP >= 8.0.2 16 | 17 | ## 安装 18 | 19 | ```bash 20 | composer require larva/laravel-whois -vv 21 | ``` 22 | 23 | ## 使用 24 | 25 | ```php 26 | $info = \Larva\Whois\Whois::lookup('baidu.com'); 27 | $info = \Larva\Whois\Whois::lookupRaw('google.com'); 28 | ``` 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "larva/laravel-whois", 3 | "description": "Whois Query Extension for Laravel.", 4 | "keywords": [ "laravel", "whois", "larva" ], 5 | "type": "library", 6 | "license": "MIT", 7 | "require": { 8 | "php": "^8.0.2", 9 | "ext-json": "*", 10 | "illuminate/console": "^9.0|^10.0|^11.0|^12.0", 11 | "illuminate/http": "^9.0|^10.0|^11.0|^12.0", 12 | "illuminate/support": "^9.0|^10.0|^11.0|^12.0", 13 | "illuminate/database": "^9.0|^10.0|^11.0|^12.0", 14 | "illuminate/queue": "^9.0|^10.0|^11.0|^12.0", 15 | "larva/support": "^2.0", 16 | "io-developer/php-whois": "^4.1", 17 | "jeremykendall/php-domain-parser": "^6.0" 18 | }, 19 | "require-dev": { 20 | "phpunit/phpunit": "^9.5" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Larva\\Whois\\": "src" 25 | } 26 | }, 27 | "autoload-dev": { 28 | "psr-4": { 29 | "Larva\\Whois\\Tests\\": "tests/" 30 | } 31 | }, 32 | "extra": { 33 | "branch-alias": { 34 | "dev-master": "1.0-dev" 35 | }, 36 | "laravel": { 37 | "providers": [ 38 | "Larva\\Whois\\WhoisServiceProvider" 39 | ], 40 | "aliases": { 41 | "Whois": "Larva\\Whois\\Whois" 42 | } 43 | } 44 | }, 45 | "scripts": { 46 | "test": "vendor/bin/phpunit" 47 | }, 48 | "prefer-stable": true 49 | } 50 | -------------------------------------------------------------------------------- /src/WhoisQuery.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class WhoisQuery 26 | { 27 | /** 28 | * 解析域名 29 | * 30 | * @param string $host 31 | * @return ResolvedDomainName 32 | * @throws IllegalDomainException|\Pdp\CannotProcessHost 33 | */ 34 | public function parseDomain(string $host): ResolvedDomainName 35 | { 36 | if (str_contains($host, '://')) { 37 | $url = parse_url($host); 38 | if (isset ($url ['host'])) { 39 | $host = $url ['host']; 40 | } 41 | } 42 | if ($host && str_contains($host, '.')) { 43 | $publicSuffixList = Rules::fromPath(__DIR__ . '/../resources/public_suffix_list.dat'); 44 | $domain = WhoisDomain::fromIDNA2008($host); 45 | return $publicSuffixList->resolve($domain); 46 | } else { 47 | throw new IllegalDomainException("Illegal domain name"); 48 | } 49 | } 50 | 51 | /** 52 | * 获取域名 53 | * @param string $domain 54 | * @return string 55 | * @throws IllegalDomainException 56 | * @throws \Pdp\CannotProcessHost 57 | */ 58 | public function getDomain(string $hostName): string 59 | { 60 | $domain = $this->parseDomain($hostName); 61 | return $domain->registrableDomain()->toString(); 62 | } 63 | 64 | /** 65 | * 查询原始 Whois 66 | * @param string $domain 67 | * @return string 68 | * @throws ConnectionException 69 | * @throws ServerMismatchException 70 | * @throws WhoisException 71 | * @throws IllegalDomainException|\Pdp\CannotProcessHost 72 | */ 73 | public function lookupRaw(string $domain): string 74 | { 75 | $domain = $this->parseDomain($domain); 76 | $whois = Factory::get()->createWhois(); 77 | return $whois->lookupDomain($domain->registrableDomain()->toString())->text; 78 | } 79 | 80 | /** 81 | * 查询 Whois Info 82 | * @param string|ResolvedDomainName $domain 83 | * @return TldInfo|null 84 | * @throws ConnectionException 85 | * @throws IllegalDomainException 86 | * @throws ServerMismatchException 87 | * @throws WhoisException 88 | * @throws CannotProcessHost 89 | */ 90 | public function lookupInfo(string|ResolvedDomainName $domain): ?TldInfo 91 | { 92 | $whois = Factory::get()->createWhois(); 93 | if (!$domain instanceof ResolvedDomainName) { 94 | $domain = $this->parseDomain($domain); 95 | } 96 | return $whois->loadDomainInfo($domain->registrableDomain()->toString()); 97 | } 98 | 99 | /** 100 | * 查询 Whois 101 | * @param string $domain 要查询的域名 102 | * @return array 103 | * @throws ConnectionException 104 | * @throws IllegalDomainException 105 | * @throws ServerMismatchException 106 | * @throws WhoisException|\Pdp\CannotProcessHost 107 | */ 108 | public function lookup(string $domain): ?array 109 | { 110 | $response = $this->lookupInfo($this->parseDomain($domain)); 111 | if(!$response) { 112 | return null; 113 | } 114 | return [ 115 | 'parser_type' => $response->parserType, 116 | 'name' => $response->domainName, 117 | 'registrar' => $response->registrar, 118 | 'owner' => $response->owner, 119 | 'whois_server' => $response->whoisServer, 120 | 'states' => $response->states, 121 | 'name_servers' => $response->nameServers, 122 | 'dnssec' => $response->dnssec, 123 | 'creation_date' => Carbon::createFromTimestamp($response->creationDate), 124 | 'expiration_date' => Carbon::createFromTimestamp($response->expirationDate), 125 | 'updated_date' => Carbon::createFromTimestamp($response->updatedDate), 126 | 'response_raw' => $response->getResponse()->text, 127 | ]; 128 | } 129 | } --------------------------------------------------------------------------------