- dale-c-anderson / whois.php
- Whois, как получить данные IP-адреса и домена в PHP
- Информация о IP
- Результат:
- Информация о домене
- Результат:
- Сервис для проверки IP и доменов
- Saved searches
- Use saved searches to filter your results more quickly
- License
- phpWhois/phpWhois
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
dale-c-anderson / whois.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
< form method = GET action =" " > |
< input type = text name =" q " value =" " > |
< pre > |
/* |
* whois.php |
*/ |
main(); |
function main () |
$ domain = $ _REQUEST [ ‘q’ ]; |
if (! $ domain ) |
return FALSE ; |
> |
if (!is_valid_domain_name( $ domain )) |
echo » Invalid query «; |
return FALSE ; |
> |
if (strlen( $ domain ) < 4 ) |
echo » No domain name is that short. «; |
return FALSE ; |
> |
if (strlen( $ domain ) > 80 ) |
echo » Too long. «; |
return FALSE ; |
> |
//echo ‘$ whois ‘ . htmlentities($domain) . «\r\n»; |
$ whois = shell_exec(» whois ‘ » . addslashes( $ domain ) . » ‘ «); |
print_r( $ whois ); |
> |
function is_valid_domain_name ( $ domain_name ) |
// Thanks to http://stackoverflow.com/a/4694816 |
return (preg_match(» /^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i «, $ domain_name ) //valid chars check |
&& preg_match(» /^.$/ «, $ domain_name ) //overall length check |
&& preg_match(» /^[^\.](\.[^\.])*$/ «, $ domain_name ) ); //length of each label |
> |
Whois, как получить данные IP-адреса и домена в PHP
Несколько примеров как в PHP получить информацию о домене и IP-адресе.
Информация о IP
Получить информацию об IP-адресе можно с помощью сервиса WHOIS REST API, отправив запрос на http://rest.db.ripe.net/search?query-string= xxx.xxx.xxx.xxx
Ответ будет в формате XML.
$ip = '213.180.193.1'; $xml = simplexml_load_string(file_get_contents('http://rest.db.ripe.net/search?query-string=' . $ip)); $array = json_decode(json_encode($xml), TRUE); $data = array(); foreach ($array['objects'] as $row) < foreach ($row as $row2) < foreach ($row2['attributes'] as $row3) < foreach ($row3 as $row4) < $data[$row4['@attributes']['name']][] = $row4['@attributes']['value']; >> > > print_r($data);
Результат:
Array( [inetnum] => Array( [0] => 213.180.193.0 - 213.180.193.255 ) [netname] => Array( [0] => YANDEX-213-180-193 ) [status] => Array( [0] => ASSIGNED PA ) [country] => Array( [0] => RU ) [descr] => Array( [0] => Yandex enterprise network [1] => Yandex enterprise network ) [admin-c] => Array( [0] => YNDX1-RIPE [1] => PGR777-RIPE ) [tech-c] => Array( [0] => YNDX1-RIPE [1] => EM3673-RIPE [2] => AUR2-RIPE ) [remarks] => Array( [0] => INFRA-AW [1] => trouble: ------------------------------------------------------ [2] => trouble: Points of contact for Yandex LLC Network Operations [3] => trouble: ------------------------------------------------------ [4] => trouble: Routing and peering issues: noc@yandex.net [5] => trouble: SPAM issues: abuse@yandex.ru [6] => trouble: Network security issues: abuse@yandex.ru [7] => trouble: Mail issues: postmaster@yandex.ru [8] => trouble: General information: info@yandex.ru [9] => trouble: ------------------------------------------------------ ) [mnt-by] => Array( [0] => YANDEX-MNT [1] => YANDEX-MNT [2] => YANDEX-MNT ) [created] => Array( [0] => 2011-08-30T09:34:57Z [1] => 2002-06-07T05:35:50Z [2] => 2012-02-09T15:59:56Z ) [last-modified] => Array( [0] => 2014-03-26T08:38:45Z [1] => 2018-08-03T07:15:46Z [2] => 2012-02-09T15:59:56Z ) [source] => Array( [0] => RIPE [1] => RIPE [2] => RIPE ) [role] => Array( [0] => Yandex LLC Network Operations ) [address] => Array( [0] => Yandex LLC [1] => 16, Leo Tolstoy St. [2] => 119021 [3] => Moscow [4] => Russian Federation ) [phone] => Array( [0] => +7 495 739 7000 ) [fax-no] => Array( [0] => +7 495 739 7070 ) [nic-hdl] => Array( [0] => YNDX1-RIPE ) [abuse-mailbox] => Array( [0] => abuse@yandex.ru ) [route] => Array( [0] => 213.180.193.0/24 ) [origin] => Array( [0] => AS13238 ) )
Информация о домене
С доменами сложнее, т.к. нужно отправлять TCP запрос (43-й порт) на WHIOS-сервер в зависимости от зоны домена, полный список whois-серверов.
Основные зоны
Зона | Сервер |
---|---|
.ru, .su, .рф | whois.tcinet.ru |
.com, .net | whois.verisign-grs.net |
.org | whois.pir.org |
$server = 'whois.tcinet.ru'; $host = 'yandex.ru'; $socket = fsockopen($server, 43); if ($socket) < fputs($socket, $host . PHP_EOL); $text = ''; while (!feof($socket)) < $text .= fgets($socket, 128); >fclose($socket); echo $text; >
Результат:
% By submitting a query to RIPN's Whois Service % you agree to abide by the following terms of use: % http://www.ripn.net/about/servpol.html#3.2 (in Russian) % http://www.ripn.net/about/en/servpol.html#3.2 (in English). domain: YANDEX.RU nserver: ns1.yandex.ru. 213.180.193.1, 2a02:6b8::1 nserver: ns2.yandex.ru. 93.158.134.1, 2a02:6b8:0:1::1 nserver: ns9.z5h64q92x9.net. state: REGISTERED, DELEGATED, VERIFIED org: YANDEX, LLC. registrar: RU-CENTER-RU admin-contact: https://www.nic.ru/whois created: 1997-09-23T09:45:07Z paid-till: 2019-09-30T21:00:00Z free-date: 2019-11-01 source: TCI Last updated on 2019-04-10T13:26:32Z
Сервис для проверки IP и доменов
Есть хороший API сервис «ip-api», возвращает информацию о домене, IP-адресе + геолакацию. Не требует регистрации и ключей, ограничение 150 запросов в минуту с одного IP-адреса, но не для коммерческого использования.
$host = 'yandex.ru'; $json = file_get_contents('http://ip-api.com/json/' . $host . '?lang=ru'); $array = json_decode($json, TRUE); print_r($array);
Array ( [as] => AS13238 YANDEX LLC [city] => Москва [country] => Россия [countryCode] => RU [isp] => Yandex enterprise network [lat] => 55.734 [lon] => 37.5883 [org] => Yandex LLC [query] => 5.255.255.80 [region] => MOW [regionName] => Москва [status] => success [timezone] => Europe/Moscow [zip] => )
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
phpWhois general repository
License
phpWhois/phpWhois
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
This package contains a Whois (RFC954) library for PHP. It allows a PHP program to create a Whois object, and obtain the output of a whois query with the lookup function.
The response is an array containing, at least, an element ‘rawdata’, containing the raw output from the whois request.
In addition, if the domain belongs to a registrar for which a special handler exists, the special handler will parse the output and make additional elements available in the response. The keys of these additional elements are described in the file HANDLERS.md.
It fully supports IDNA (internationalized) domains names as defined in RFC3490, RFC3491, RFC3492 and RFC3454.
It also supports ip/AS whois queries which are very useful to trace SPAM. You just only need to pass the doted quad ip address or the AS (Autonomus System) handle instead of the domain name. Limited, non-recursive support for Referral Whois (RFC 1714/2167) is also provided.
phpWhois requires PHP 5.3 or better with OpenSSL support to work properly.
Without SSL support you will not be able to query domains which do not have a whois server but that have a https based whois.
php composer.phar require «phpwhois/phpwhois»:»~4.0″
Latest development version
php composer.phar require «phpwhois/phpwhois»:»dev-master»
// Load composer framework if (file_exists(__DIR__ . '/vendor/autoload.php')) < require(__DIR__ . '/vendor/autoload.php'); > use phpWhois\Whois; $whois = new Whois(); $query = 'example.com'; $result = $whois->lookup($query,false); echo " "; print_r($result); echo " ";
If you provide the domain name to query in UTF8, then you must use:
If the query string is not in UTF8 then it must be in ISO-8859-1 or IDNA support will not work.
You can use phpWhois to query domain names, ip addresses and other information like AS, i.e, both of the following examples work:
use phpWhois\Whois; $whois = new Whois(); $result = $whois->lookup('example.com'); $whois = new Whois(); $result = $whois->lookup('62.97.102.115'); $whois = new Whois(); $result = $whois->lookup('AS220');
Using special whois server
Some registrars can give special access to registered whois gateways in order to have more fine control against abusing the whois services. The currently known whois services that offer special acccess are:
The new ripe whois server software support some special parameters that allow to pass the real client ip address. This feature is only available to registered gateways. If you are registered you can use this service when querying ripe ip addresses that way:
use phpWhois\Whois; $whois = new Whois(); $whois->useServer('uk','whois.ripe.net?-V, '); $result = $whois->lookup('62.97.102.115');
This server is also using the new ripe whois server software and thus works the same way. If you are registered you can use this service when querying .il domains that way:
use phpWhois\Whois; $whois = new Whois(); $whois->useServer('uk','whois.isoc.org.il?-V, '); $result = $whois->lookup('example.co.uk');
They offer what they call WHOIS2 (see http://www.nominet.org.uk/go/whois2 ) to registered users (usually Nominet members) with a higher amount of permited queries by hour. If you are registered you can use this service when querying .uk domains that way:
use phpWhois\Whois; $whois = new Whois(); $whois->useServer('uk','whois.nic.uk:1043? '); $result = $whois->lookup('example.co.uk');
This new feature also allows you to use a different whois server than the preconfigured or discovered one by just calling whois->useServer and passing the tld and the server and args to use for the named tld. For example you could use another whois server for .au domains that does not limit the number of requests (but provides no owner information) using this:
use phpWhois\Whois; $whois = new Whois(); $whois->useServer('au','whois-check.ausregistry.net.au');
use phpWhois\Whois; $whois = new Whois(); $whois->useServer('be','whois.tucows.com');
to avoid the restrictions imposed by the .be whois server
use phpWhois\Whois; $whois = new Whois(); $whois->useServer('ip','whois.apnic.net');
to lookup an ip address at specific whois server (but loosing the ability to get the results parsed by the appropiate handler)
useServer can be called as many times as necessary. Please note that if there is a handler for that domain it will also be called but returned data from the whois server may be different than the data expected by the handler, and thus results could be different.
If you just want to know if a domain is registered or not but do not care about getting the real owner information you can set:
this will tell phpWhois to just query one whois server. For .com , .net and .tv domains and ip addresses this will prevent phpWhois to ask more than one whois server, you will just know if the domain is registered or not and which is the registrar but not the owner information.
PHPWhois will assume that all whois servers return UTF-8 encoded output, if some whois server does not return UTF-8 data, you can include it in the NON_UTF8 array in whois.servers.php
Workflow of getting domain info
- Call method phpWhois\Whois::lookup() with domain name as parameter
- If second parameter of method is true (default), phpWhois will try to convert the domain name to punycode
- If domain is not listed in predefined handlers ( WHOIS_SPECIAL at src/whois.servers.php ), try to query [tld].whois-servers.net. If it has ip address, assume that it is valid whois server
- Try to query found whois server or fill response array with unknown() method
There is an extended class called phpWhois\Utils which contains a debugging function called showObject() , if you showObject($result) it will output the total layout of the returned object to the web browser.
The latest version of the package and a demo script resides at https://github.com/phpWhois/phpWhois
If you want to add support for new TLD, extend functionality or correct a bug, feel free to create a new pull request at Github’s repository https://github.com/phpWhois/phpWhois