返回域名php的名称服务器数组


Returning array of nameservers for domain php

我想在php中返回一个域名服务器,但是我只得到一个域名服务器放在数组中。

这是我使用的代码:

//get auth ns datat
$authnsData = dns_get_record($domain, DNS_NS);
//put the results into a nice array
foreach ($authnsData as $nsinfo)
{
    $authns = array(
        'nsdata' => array(
            'nameserver' => $nsinfo['target'],
            'ip' => $this->getnsIP($nsinfo['target']),
            'location' => $this->getipLocation($this->getnsIP($nsinfo['target'])),
         ),
    );
    return $authns;
}

得到的结果是:

Array
(
    [nsdata] => Array
        (
            [nameserver] => ns-us.1and1-dns.org
            [ip] => 217.160.83.2
            [location] => 
        )
)

假设一个域有2个或更多的域名服务器,我只把其中一个添加到数组中。

如果您想测试它以找出问题,代码在这个文件中:https://github.com/Whoisdoma/core/blob/master/src/Whoisdoma/Controllers/DNSWhoisController.php#L38

函数是getAuthNS和LookupAuthNS。在有人建议使用for循环之前,我已经尝试了for ($num = 0;)类型的循环。

  1. 你返回得太早,所以你的循环只运行一次迭代。
  2. 你在每次迭代中分配一个新的数组给$authns,而不是推入它。

试试这段代码:

foreach ($authnsData as $nsinfo)
{
    $authns[] = [
        'nsdata' => [
            'nameserver' => $nsinfo['target'],
            'ip'         => $this->getnsIP($nsinfo['target']),
            'location'   => $this->getipLocation($this->getnsIP($nsinfo['target'])),
         ],
    ];
}
return $authns;

BTW,没有必要运行getnsIP两次。你可以用这个代替:

foreach ($authnsData as $nsinfo)
{
    $nameserver = $nsinfo['target'];
    $ip         = $this->getnsIP($nameserver);
    $location   = $this->getipLocation($ip);
    $authns[] = ['nsdata' => compact('nameserver', 'ip', 'location')];
}
return $authns;