10

So anyways, I'm working on a small PHP website/script, and as one of the features I'd like to be able to run a WHOIS lookup on the current domain the PHP script is running on.

Ideally, it would be one function that I could call and in the function it would run the WHOIS, and then echo the results to the screen. It would take in the URL of the site to run the WHOIS lookup on, or it would just run it on the current URL/Domain (which is what I want), although I can feed it a variable for the website domain if need be.

I don't know much about WHOIS lookups (well, I know what they do, I just don't know how to run them in PHP), but I'd also be fine with having to query another website (even one of my own if you can give me the code for it).

Whatever works, please just let me know! The main thing is that, I'd prefer it to fit all in one function, and it definitely must fit in one PHP file/document.

Alper
  • 1
  • 12
  • 39
  • 78

8 Answers8

12

With php you can use shell_exec to execute the whois command.

    <?php
    $whois = shell_exec("whois domain.net");
    echo '<pre>';
    print_r($whois);
    ?>
Pavel Petrov
  • 730
  • 9
  • 11
  • 4
    No, do not shell out to run a whois command, use the libraries inside your programming language or open a TCP socket on port 43, see RFC3912 – Patrick Mevzek Jan 04 '18 at 15:23
  • 2
    Because you do not need to, as you have everything inside the language to do it: 1) there are whois libraries in PHP and 2) even if they weren't, whois is just a TCP connection on port 43 so this is easy to do in any language. Calling a shell instead gives you all these drawbacks: less performance (more use of RAM, less execution speed, etc.), security (here you do not even know which whois command you run, the path is not absolute), limits (where the code exists there may be no shell or no shell access allowed, or the whois command not being installed, etc.). There are no advantages! – Patrick Mevzek Feb 10 '20 at 14:56
  • 1
    @PatrickMevzek so why don't you show a better way instead of giving these generalities? – c00000fd Mar 07 '20 at 19:14
  • @c00000fd What don't you feel specific enough in my first comment being "use the libraries inside your programming language or open a TCP socket on port 43," ? – Patrick Mevzek Mar 08 '20 at 01:08
10

This should do exactly what you want... http://www.phpwhois.org/

I've used this class before, doing exactly what you want!

Mingle
  • 846
  • 6
  • 13
  • Worked amazingly, thank you! I had to change it a bit to fit it into my site, but still works fine! – Alper Jul 18 '11 at 02:37
  • Glad it worked for you! It should fit into the space/file requirements that you had. If not, I'm sure we could all help you do that. – Mingle Jul 18 '11 at 02:40
  • 2
    Be aware that no matter what script you use, most WHOIS servers will enforce a strict limit on queries (these vary by server and TLD). So if you need to do bulk queries, you'll start seeing some version of the "WHOIS LIMIT EXCEEDED" error. – Chadwick Meyer Jun 11 '15 at 21:01
  • To avoid such limitation, one could use this [whois-api](http://whois-api.domaininformation.de/). – Markus Malkusch Nov 16 '16 at 10:55
5

To take Pavels answer one step further - this will break it down in to an array:

$whois = shell_exec("whois 45.118.135.255");

$result = explode("\n",$whois);

$out = array();
foreach ($result as $line){
    if (substr($line,0,1) == '%' || substr($line,0,1) == '#'){ continue; }

    $ps = explode(':',$line);
    $out[trim($ps[0])] = trim($ps[1]);
}

print '<pre>'; print_r($out); print '</pre>';
Antony
  • 3,875
  • 30
  • 32
  • 2
    No, do not shell out to run a whois command, use the libraries inside your programming language or open a TCP socket on port 43, see RFC3912 – Patrick Mevzek Jan 04 '18 at 15:23
1

There are some third party packages:

First one : io-developer/php-whois

composer require io-developer/php-whois

Usage:

// How to get summary about domain:

<?php

use Iodev\Whois\Factory;

// Creating default configured client
$whois = Factory::get()->createWhois();

// Checking availability
if ($whois->isDomainAvailable("google.com")) {
    print "Bingo! Domain is available! :)";
}

// Supports Unicode (converts to punycode)
if ($whois->isDomainAvailable("почта.рф")) {
    print "Bingo! Domain is available! :)";
}

// Getting raw-text lookup
$response = $whois->lookupDomain("google.com");
print $response->text;

// Getting parsed domain info
$info = $whois->loadDomainInfo("google.com");
print_r([
    'Domain created' => date("Y-m-d", $info->creationDate),
    'Domain expires' => date("Y-m-d", $info->expirationDate),
    'Domain owner' => $info->owner,
]);

// Exceptions on domain lookup:

<?php

use Iodev\Whois\Factory;
use Iodev\Whois\Exceptions\ConnectionException;
use Iodev\Whois\Exceptions\ServerMismatchException;
use Iodev\Whois\Exceptions\WhoisException;

try {
    $whois = Factory::get()->createWhois();
    $info = $whois->loadDomainInfo("google.com");
    if (!$info) {
        print "Null if domain available";
        exit;
    }
    print $info->domainName . " expires at: " . date("d.m.Y H:i:s", $info->expirationDate);
} catch (ConnectionException $e) {
    print "Disconnect or connection timeout";
} catch (ServerMismatchException $e) {
    print "TLD server (.com for google.com) not found in current server hosts";
} catch (WhoisException $e) {
    print "Whois server responded with error '{$e->getMessage()}'";
}

If you are using Laravel : laravel-whois

composer require larva/laravel-whois -vv

php artisan migrate

Usage:

$info = \Larva\Whois\Whois::lookup('baidu.com', true);
$info = \Larva\Whois\Whois::lookupRaw('google.com');
Tugrul Yildirim
  • 329
  • 2
  • 8
0

I found it here: https://whoisfreaks.com/documentation/api/whois-api.html. You can get Whoislook up by using PHP thru this code snippet:

     'https://api.whoisfreaks.com/v1.0/whois?whois=live&domainName=jfreaks.com&apikey=Your API_Key',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

I hope it'll work for you too.

Rameez
  • 407
  • 3
  • 11
0

Best thing to do would be to use pywhois. Though you say Python in the question title but don't mention it in the post. If you actually need PHP, I'm sure there's something equivalent for that.

fletom
  • 1,998
  • 13
  • 17
  • Yeah, I think I needed PHP more than Python, but I think I'll still have a use for the pywhois, just not in this specific project. Thank you! – Alper Jul 18 '11 at 02:37
0

Making WHOIS lookups is a simple ordeal of sending a request string over TCP port 43, which can be done with plain PHP socket functions, no need to spawn shells or use any external tools/libraries. While the queries themselves are simple, there are some things to consider on how the queries should be carried out.

For IP lookups, you need to query the correct regional WHOIS server of whom the IP block in question is assigned to, these are:

  • ARIN for North America
  • RIPE for Europe/Middle East/Central Asia
  • APNIC for Asia-Pacific
  • LACNIC for Latin America/Caribbean
  • AFRINIC for Africa

For domain lookups, you need to query the correct WHOIS server for a given TLD. You also should query the domain registrar's WHOIS server to acquire correct information for the domain itself.


See this simple example for basic WHOIS functionality: whois.php (PHP >= 4.0)
It has all the considerations baked in so correct results are always returned.

Features:

  • Both domain and IP lookups
  • Input check & sanitization
  • HTML or plaintext (&txt=1) results
  • Correctly returns worldwide IP lookup results from the regional IP WHOIS server the block is assigned to
  • Can follow the root TLD WHOIS server to Registrar's WHOIS for domain requests if applicable for more accurate information
  • In addition to having a static list of WHOIS servers and falling back to the generic whois.nic.$tld paradigm, can be configured to lookup the IANA database for the correct WHOIS server for any given TLD at the cost of one extra query
  • Can support Internationalized domain names (IDN)
  • Can support both UTF-8 and ISO-8859-1 responses from servers
anzz1
  • 91
  • 1
  • 4
-1

Try the Function Which is available in github gist

https://gist.github.com/ManojKiranA/4b034659e85fa02308ad9bdcdd05629c

For the full list of TLDs/Whois servers see http://www.iana.org/domains/root/db/ and http://www.whois365.com/en/listtld/

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43