回到域名服务器的阵列域的PHP [英] Returning array of nameservers for domain php

查看:141
本文介绍了回到域名服务器的阵列域的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要回在PHP中域的域名服务器,但我只得到一个域名服务器把数组中的

这是code我使用的:

  //获取权威性NS datat
$ authnsData = dns_get_record($域,DNS_NS);//把结果放到一个不错的数组
的foreach($ authnsData为$ nsinfo)
{
    $ authns =阵列(
        'NSData的'=>阵列(
            '名称服务器'=> $ nsinfo ['目标'],
            IP=> $这个 - > getnsIP($ nsinfo ['目标'])
            位置= GT; $这个 - > getipLocation($这个 - > getnsIP($ nsinfo ['目标']))
         )
    );    返回$ authns;
}

我得到的结果是:

 阵列

    [NSData的] =>排列
        (
            [域名服务器] => ns-us.1and1-dns.org
            [IP] => 217.160.83.2
            [位置] =>
        ))

比方说一个域有2个或更多域名服务器,我只得到那些添加到阵列中的一个。

如果你想测试一下,找出问题,code是在这个文件中:<一href=\"https://github.com/Whoisdoma/core/blob/master/src/Whoisdoma/Controllers/DNSWhoisController.php#L38\" rel=\"nofollow\">https://github.com/Whoisdoma/core/blob/master/src/Whoisdoma/Controllers/DNSWhoisController.php#L38

的功能是getAuthNS和LookupAuthNS。之前有人建议使用一个for循环,我已经尝试了为($ NUM = 0)。输入回路


解决方案

  1. 您正在返回太早,让您的循环永远只能运行一个单一的迭代。

  2. 您是在每个迭代分配一个新的数组 $ authns ,而不是推了进去。


试试这件作品code,而不是:

 的foreach($ authnsData为$ nsinfo)
{
    $ authns [] = [
        'NSData的'=&GT; [
            '名称服务器'=&GT; $ nsinfo ['目标'],
            IP=&GT; $这个 - &GT; getnsIP($ nsinfo ['目标'])
            位置= GT; $这个 - &GT; getipLocation($这个 - &GT; getnsIP($ nsinfo ['目标']))
         ]
    ];
}返回$ authns;


BTW,没有必要跑 getnsIP 的两倍。你可以用这个代替:

 的foreach($ authnsData为$ nsinfo)
{
    $域名服务器= $ nsinfo ['目标'];
    $ IP = $这个 - &GT; getnsIP($域名服务器);
    $位置= $这个 - &GT; getipLocation($ IP);    $ authns [] = ['NSData的'=&GT;紧凑的('域名服务器','IP','位置')];
}返回$ authns;

I want to return the nameservers of a domain in php, however I'm only getting one nameserver put in the array.

This is the code I am using:

//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;
}

The result I'm getting is:

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

)

Lets say a domain has 2 or more nameservers, I'm only getting one of those added to the array.

If you would like to test it out to find out the issue, the code is in this file: https://github.com/Whoisdoma/core/blob/master/src/Whoisdoma/Controllers/DNSWhoisController.php#L38

The function is getAuthNS, and LookupAuthNS. Before anyone suggests using a for loop, I have tried a for ($num = 0;) type loop.

解决方案

  1. You're returning too early, so that your loop only ever runs a single iteration.
  2. You're assigning a new array to $authns in every iteration, instead of pushing into it.


Try this piece of code instead:

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

return $authns;


BTW, there's no need to run getnsIP twice. You can use this instead:

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

    $authns[] = ['nsdata' => compact('nameserver', 'ip', 'location')];
}

return $authns;

这篇关于回到域名服务器的阵列域的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆