1

我需要从域中获取 MX 记录,但只有具有最高优先级(最低编号)的记录。

我已经玩了相当多的游戏,但无法弄清楚如何只返回一个结果。

   $results = dns_get_record($domain, DNS_MX);

   foreach ($results as $result)
   {
    $A = dns_get_record($result['target'], DNS_A);
    foreach ($A as $ip)
    { 
             echo $ip['ip'];
    }
   }

这给了我想要的结果,但对于域拥有的每条 MX 记录。

如果有人能指出我正确的方向,那就太好了!

干杯!

4

1 回答 1

2

使用array_column()函数收集所有的优先级,然后使用array_filter拉出最小的:

// get all the results
$results = dns_get_record($domain, DNS_MX);
// find the lowest value in the "pri" column
$target_pri = min(array_column($results, "pri"));
$highest_pri = array_filter(
    $results,
    // keep anything that matches the lowest (could be more than one)
    function($item) use($target_pri) {return $item["pri"] === $target_pri;}
);
// now loop through each of them, finding all their IP addresses
foreach ($highest_pri as $mx) {
    echo "$mx[target]: ";
    $results = dns_get_record($mx["target"], DNS_A);
    foreach ($results as $a) {
        echo "$a[ip] ";
    }
    echo "\n";
}
于 2018-09-26T16:44:59.730 回答