2016-04-21 102 views
1

如何使用whois響應數據在php上獲得域名過期日期?如何使用whois響應數據在PHP上獲得域名過期日期?

通常我使用whois響應來檢查域的可用性。

例如:檢查DRGDRGDRGRGDRG.COM如果whois響應數據有詞No match for這意味着此域可用。

No match for domain "DRGDRGDRGRGDRG.COM". 

但現在我要檢查域使用這樣

Domain Name: GOOGLE.COM 
Registrar: MARKMONITOR INC. 
Sponsoring Registrar IANA ID: 292 
Whois Server: whois.markmonitor.com 
Referral URL: http://www.markmonitor.com 
Name Server: NS1.GOOGLE.COM 
Name Server: NS2.GOOGLE.COM 
Name Server: NS3.GOOGLE.COM 
Name Server: NS4.GOOGLE.COM 
Status: clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited 
Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited 
Status: clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited 
Status: serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited 
Status: serverTransferProhibited https://icann.org/epp#serverTransferProhibited 
Status: serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited 
Updated Date: 20-jul-2011 
Creation Date: 15-sep-1997 
Expiration Date: 14-sep-2020 

我如何能做到這一點的whois響應數據到期日期?謝謝

+0

也許有趣? http://stackoverflow.com/questions/36817/who-provides-a-whois-api –

回答

1

這不是一個簡單的解析響應字符串的問題,因爲它可能聽起來在第一時間,因爲域名註冊商以不同的格式提供信息。

我想我們這裏有兩種選擇:

  1. 使用一些庫,當它出現故障解析字符串
  2. 盡一切自己,不使用任何庫,並解析輸出。

我建議從一些圖書館開始,但我真的不知道'完美'的一個。我要去嘗試phpWhois。如果失敗,它會提供原始數據以嘗試解析它。

首先你需要安裝庫。我這樣做使用Composer。下面是我composer.json文件

{ 
    "require": { 
    "phpwhois/phpwhois":"dev-master", 
    "mso/idna-convert": "0.9.1" 
    } 
} 

需要注意的是最新版本phpWhois不與最新版本idna-convert工作,這就是爲什麼我在我的要求來指定。

執行composer install下載庫。

最後的PHP腳本來查詢域名:

<?php 

require(__DIR__ . '/vendor/autoload.php'); 

use phpWhois\Whois; 

$whois = new Whois(); 
$whois->deepWhois = true; 

$query = isset($argv[1]) ? $argv[1] : 'google.com'; 
$result = $whois->lookup($query); 

$registered = isset($result['regrinfo']['registered']) && $result['regrinfo']['registered'] == 'yes'; 
if (!$registered) { 
    echo 'Domain: '.$query.' not registered.'.PHP_EOL; 
} else { 
    if (isset($result['regrinfo']['domain']['expires'])) { 
     echo 'Domain: '.$query.PHP_EOL; 
     echo 'Expired: '.$result['regrinfo']['domain']['expires'].PHP_EOL; 
    } else { 
     echo 'Domain: '.$query.PHP_EOL; 
     echo 'Trying to find expires date...'.PHP_EOL; 
     foreach ($result['rawdata'] as $raw) { 
      if (strpos($raw, 'Expiry Date:') !== false) { 
       echo 'Expired: '.trim(explode(':', $raw)[1]).PHP_EOL; 
      } 
     } 
    } 
} 

它採用域名作爲第一個腳本參數$argv[1]

如果庫沒有解析註冊商的結果,我們嘗試手動解析它。我添加了一個簡單的檢查

if (strpos($raw, 'Expiry Date:') !== false) { 
    echo 'Expired: '.trim(explode(':', $raw)[1]).PHP_EOL; 
} 

你可以在原始響應數據搜索「有效期」還是基於與分析數據,如果你將你的經驗,一些更好的邏輯。完美的圖書館應該這樣做,但有時會失敗。

相關問題