【问题标题】:Returning array of nameservers for domain php返回域 php 的名称服务器数组
【发布时间】:2014-08-20 00:53:36
【问题描述】:

我想在 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;) 类型的循环。

【问题讨论】:

    标签: php arrays laravel dns


    【解决方案1】:
    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;
    

    顺便说一句,没有必要运行getnsIP 两次。你可以改用这个:

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

    【讨论】:

      猜你喜欢
      • 2013-07-12
      • 2011-11-04
      • 1970-01-01
      • 2014-11-02
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      相关资源
      最近更新 更多