我需要从一个域中获取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记录来说。
如果有人能指出我正确的方向,那就太好了!
干杯!
答案 0 :(得分: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";
}