2010-06-08 43 views
1

我正在使用dns_get_record設置dns查找表單。我將其設置爲檢查輸入域的A記錄和MX記錄。但是,我希望它也顯示所顯示的MX記錄的IP地址。這可能嗎?dns_get_record問題

回答

1

不,至少不是一步到位。您必須爲MX記錄的「目標」執行另一個DNS請求,這是郵件服務器的「真實」地址。

一個簡單的腳本可能看起來像這樣

$email = "[email protected]"; 
list($tmp, $email) = explode("@", $email); // Gets the domain name 

$dns = dns_get_record($email, DNS_MX); 
if(count($dns) <= 0) 
    die("Error looking up dns information."); // Return value is an empty array if there aren't any MX records but domain exists 

// Looks up the first returned MX (note that there can be more than one) 
// Each MX record has a 'pri' value where the lowest value is the record with the highest priority 
$mx = dns_get_record($dns[0]['target'], DNS_A); 
if(count($mx) <= 0) 
    die("Error looking up mail server."); 
$mx = $mx[0]['ip']; 

一個完全成熟的A和MX記錄顯示腳本

$domain = "google.com"; 

$dns = dns_get_record($domain, DNS_ANY); 
foreach($dns as $d) { 
    // Only print A and MX records 
    if($d['type'] != "A" and $d['type'] != "MX") 
     continue; 
    // First print all fields 
    echo "--- " . $d['host'] . ": <br />\n"; 
    foreach($d as $key => $value) { 
     if($key != "host") // Don't print host twice 
      echo " {$key}: {$value} <br />\n"; 
    } 
    // Print type specific fields 
    switch($d['type']) { 
     case 'A': 
      // Display annoying message 
      echo "A records always contain an IP address. <br />\n"; 
      break; 
     case 'MX': 
      // Resolve IP address of the mail server 
      $mx = dns_get_record($d['target'], DNS_A); 
      foreach($mx as $server) { 
       echo "The MX record for " . $d['host'] . " points to the server " . $d['target'] . " whose IP address is " . $server['ip'] . ". <br />\n"; 
      } 
      break; 
    } 
} 
+0

你能提供,也許我將如何設置了一個例子嗎? – Batfan 2010-06-08 20:44:12

+0

添加了(未經測試的)示例。 – svens 2010-06-08 20:55:28

+0

嗯,一直在試圖實現這個到我的腳本,我有問題。這是我使用的基本設置 http://bit.ly/dxxush – Batfan 2010-06-08 21:06:21