Skip to content

Commit

Permalink
New method data() to parse the domain info.
Browse files Browse the repository at this point in the history
  • Loading branch information
shapito27 authored and shapito27 committed Oct 24, 2020
1 parent ab95a73 commit 7103589
Show file tree
Hide file tree
Showing 5 changed files with 1,595 additions and 3,408 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# PHPStorm
.idea/
/vendor
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,27 @@ if ($domain->isAvailable()) {
echo "Domain is registered\n";
}

```
```

A more complete example:

```php

<?php
require_once __DIR__.'/../vendor/autoload.php';

$sld = 'nabi.ir';

try {
$domain = new Phois\Whois\Whois($sld);
} catch (InvalidArgumentException $e) {
die($e->getMessage()."\n");
}

if ($domain->isAvailable()) {
echo "Domain is available\n";
} else {
echo "Domain is registered\n";
}

```
16 changes: 16 additions & 0 deletions examples/usage-example2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
require_once __DIR__.'/../vendor/autoload.php';

$sld = 'nabi.ir';

try {
$domain = new Phois\Whois\Whois($sld);
} catch (InvalidArgumentException $e) {
die($e->getMessage()."\n");
}

if ($domain->isAvailable()) {
echo "Domain is available\n";
} else {
echo "Domain is registered\n";
}
95 changes: 93 additions & 2 deletions src/Phois/Whois/Whois.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ public function __construct($domain)
throw new \InvalidArgumentException("Invalid $domain syntax");
// setup whois servers array from json file
$this->servers = json_decode(file_get_contents( __DIR__.'/whois.servers.json' ), true);

if (!$this->isValid())
throw new \InvalidArgumentException("Domain name isn't valid!");
}


/**
* @param string, domain whois information
*/
public function info()
{
if ($this->isValid()) {
Expand Down Expand Up @@ -125,6 +131,88 @@ public function info()
}
}

public function data()
{
$result = new \stdClass();
$result->status = 0;
$result->message = 'error';
$result->data = [];
try {
$info = $this->info();

$not_found_string = FALSE;
if (isset($this->servers[$this->TLDs][1])) {
$not_found_string = $this->servers[$this->TLDs][1];
}

// Check if this domain is not found (available for registration).
if ($not_found_string) {
if (strpos($info, $not_found_string) !== false) {
$result->status = 2;
$result->message = 'not_found';
}
}

// Make sure the status is still the default value, and the not_found
// string value are exists before extracting the data from info.
if (($result->status == 0) && ($not_found_string)) {
$exploded_info = explode("\n", $info);
$data = [];
foreach ($exploded_info as $lineNumber => $line) {
if (strpos($line, 'Creation Date:') !== false) {
$data['creation_date'] = trim(str_replace('Creation Date:', '', $line));
}

if (strpos($line, 'Registry Expiry Date:') !== false) {
$data['expiration_date'] = trim(str_replace('Registry Expiry Date:', '', $line));
}

if (strpos($line, 'Updated Date:') !== false) {
$data['update_date'] = trim(str_replace('Updated Date:', '', $line));
}

if (strpos($line, 'Registry Domain ID:') !== false) {
$data['registry_domain_id'] = trim(str_replace('Registry Domain ID:', '', $line));
}

if (strpos($line, 'Registrar:') !== false) {
if (!isset($data['registrar'])) {
$data['registrar'] = [];
}
$data['registrar']['name'] = trim(str_replace('Registrar:', '', $line));
}

if (strpos($line, 'Registrar IANA ID:') !== false) {
if (!isset($data['registrar'])) {
$data['registrar'] = [];
}
$data['registrar']['id'] = trim(str_replace('Registrar IANA ID:', '', $line));
}

if (strpos($line, 'Name Server:') !== false) {
if (!isset($data['name_servers'])) {
$data['name_servers'] = [];
}
$data['name_servers'][] = trim(str_replace('Name Server:', '', $line));
}
}

// If there are data, we will count this as registered.
if (count($data) > 0) {
$result->status = 1;
$result->message = 'found';
$result->data = $data;
}
}

} catch (Exception $e) {
$result->status = -1;
$result->message = 'exception';
}

return $result;
}

public function htmlInfo()
{
return nl2br($this->info());
Expand Down Expand Up @@ -153,7 +241,10 @@ public function getSubDomain()
{
return $this->subDomain;
}


/**
* @return boolean, true for domain avaliable, false for domain registered
*/
public function isAvailable()
{
$whois_string = $this->info();
Expand Down
Loading

0 comments on commit 7103589

Please sign in to comment.