PHP Velho Oeste 2024

dns_get_record

(PHP 5, PHP 7, PHP 8)

dns_get_recordホスト名に関連する DNS リソースレコードを取得する

説明

dns_get_record(
    string $hostname,
    int $type = DNS_ANY,
    array &$authoritative_name_servers = null,
    array &$additional_records = null,
    bool $raw = false
): array|false

指定した hostname に関連づけられた DNS リソースレコードを取得します。

パラメータ

hostname

hostname は、正しい DNS ホスト名、すなわち "www.example.com" のようなものでなければなりません。 in-addr.arpa 形式の表記を用いた逆引き検索は可能ですが、 たいていは gethostbyaddr() を用いるほうが適当です。

注意:

DNS の標準規格により、メールアドレスは user.host 形式で渡されます (たとえば、hostmaster.example.comhostmaster@example.com の代わりとなります)。 この値をしっかり確認し、mail() のような関数で 利用する前には必要なら変更を加えることを忘れないようにしてください。

type

デフォルトでは、dns_get_record()hostname に関連するすべてのリソースレコードを検索します。 これを制限するには、オプションの type パラメータを指定してください。 type は以下のうちのいずれかです。 DNS_A, DNS_CNAME, DNS_HINFO, DNS_CAA, DNS_MX, DNS_NS, DNS_PTR, DNS_SOA, DNS_TXT, DNS_AAAA, DNS_SRV, DNS_NAPTR, DNS_A6, DNS_ALL または DNS_ANY

注意:

プラットフォーム依存の libresolv のおかしな挙動のせいで、 DNS_ANY が常にすべてのレコードを返すとは 限りません。速度は遅くなりますが、DNS_ALL のほうがより確実にすべてのレコードを取得できます。

注意:

Windows では、DNS_CAA はサポートされていませんし、 DNS_A6 は実装されていません。

authoritative_name_servers

参照で渡し、 Authoritative Name Servers のリソースレコードが格納されます。

additional_records

参照で渡し、 Additional Records が格納されます。

raw

type は生のDNSのタイプIDとして解釈されます。 (DNS_* 定数は使えません) 戻り値には手動でパースが必要な data キーが含まれます。

戻り値

この関数は、連想配列を要素にもつ配列を返します。 失敗した場合に false を返します。 それぞれの連想配列には 少なくとも 以下のキーが含まれています:

基本 DNS 属性
属性 意味
host これ以降の関連するデータが参照する DNS 名。
class dns_get_record() は Internet クラスのレコードのみを返すので、このパラメータは常に IN を返します。
type レコード型を表す文字列。type の値に応じて、 結果の配列には追加の属性が含まれます。以下の表を参照ください。
ttl このレコードの"有効期限 (Time To Live)"。レコードの本来の ttl と一致するとは 限りません。むしろ、 ネームサーバーへのクエリにかかった時間をそこから引いたものに一致します。

'type' に応じて連想配列に追加される項目
レコード型 追加項目
A ip: ドット区切り 10 進数形式の IPv4 アドレス。
MX pri: メールエクスチェンジャの優先度。 数字が小さいほど優先度が高い。 target: メールエクスチェンジャの FQDN 。 dns_get_mx() も参照ください。
CNAME target: レコードのエイリアスの対象となっている場所の FQDN 。
NS target: このホスト名に対する権威をもっているネームサーバーの FQDN 。
PTR target: レコードが指している、DNS 名前空間内の場所
TXT txt: このレコードに関連付けられている任意の文字列。
HINFO cpu: このレコードが参照しているマシンの CPU を識別する IANA 番号。 os: このレコードが参照しているマシン上の OS を識別する IANA 番号。 これらの値の意味については、IANA の » Operating System Names を参照ください。
CAA flags: 1バイトのビットフィールド; 現在はビット0だけが定義されており、'critical' を意味します。 他のビットは予約されており、無視されるべきです。 tag: CAA タグの名前 (alphanumeric な ASCII 文字列). value: CAA タグの値 (バイナリ文字列, サブフォーマットの使用可) 詳細は、» RFC 6844 を参照ください。
SOA mname: リソースレコードの元となるマシンの FQDN 。 rname: このドメインの管理責任者の Email アドレス。 serial: ドメインのシリアル番号。 refresh: セカンダリネームサーバーがこのドメインのコピーを更新する際に参照するリフレッシュ間隔(秒単位)。 retry: リフレッシュが失敗した際に 2 度目のリフレッシュを試みるまでの間隔(秒単位) expire: セカンダリネームサーバーが、ゾーンデータの リフレッシュに失敗した場合にコピーのデータを破棄せず持ち続ける期間 (秒単位)。 minimum-ttl: クライアントが、 一度取得したデータを再リクエストすることなしに利用できる最小期間(秒単位)。 個々のリソースレコードによって上書きが可能。
AAAA ipv6: IPv6 アドレス。
A6 masklen: chain で指定された対象から引き継ぐビット長。 ipv6: chain とマージするためのこのレコードのアドレス。 chain: ipv6 データとマージするための親レコード。
SRV pri: (Priority) 値が小さいものが優先されます。 weight: 同じ優先順位の targets からランダムに選択する際の重み。 target および port: リクエストされたサービスが存在するホスト名とポート。 詳細は » RFC 2782 を参照ください。
NAPTR order および pref: 上の pri および weight と同じ。 flags, services, regex, および replacement: » RFC 2915 で定義されるパラメータ。

変更履歴

バージョン 説明
7.0.16, 7.1.2 CAA レコード型のサポートが追加されました。

例1 dns_get_record() の使用

<?php
$result
= dns_get_record("php.net");
print_r($result);
?>

上の例の出力は、 たとえば以下のようになります。

Array
(
    [0] => Array
        (
            [host] => php.net
            [type] => MX
            [pri] => 5
            [target] => pair2.php.net
            [class] => IN
            [ttl] => 6765
        )

    [1] => Array
        (
            [host] => php.net
            [type] => A
            [ip] => 64.246.30.37
            [class] => IN
            [ttl] => 8125
        )

)

例2 dns_get_record() と DNS_ANY の使用

MX レコードが解決されれば、たいていはメールサーバーの IP アドレスを 取得したくなるものです。そのため、dns_get_record()additional_records に関連するレコードを含めて返します。 また、authoritative_name_servers には 権威のあるネームサーバーのリストを含めて返します。

<?php
/* php.net の "ANY" レコードを要求し、
それに付随する情報を格納した配列を
作成する。
$authns にはネームサーバーの一覧が、
また $addtl には追加レコードが
それぞれ格納される。 */
$result = dns_get_record("php.net", DNS_ANY, $authns, $addtl);
echo
"Result = ";
print_r($result);
echo
"Auth NS = ";
print_r($authns);
echo
"Additional = ";
print_r($addtl);
?>

上の例の出力は、 たとえば以下のようになります。

Result = Array
(
    [0] => Array
        (
            [host] => php.net
            [type] => MX
            [pri] => 5
            [target] => pair2.php.net
            [class] => IN
            [ttl] => 6765
        )

    [1] => Array
        (
            [host] => php.net
            [type] => A
            [ip] => 64.246.30.37
            [class] => IN
            [ttl] => 8125
        )

)
Auth NS = Array
(
    [0] => Array
        (
            [host] => php.net
            [type] => NS
            [target] => remote1.easydns.com
            [class] => IN
            [ttl] => 10722
        )

    [1] => Array
        (
            [host] => php.net
            [type] => NS
            [target] => remote2.easydns.com
            [class] => IN
            [ttl] => 10722
        )

    [2] => Array
        (
            [host] => php.net
            [type] => NS
            [target] => ns1.easydns.com
            [class] => IN
            [ttl] => 10722
        )

    [3] => Array
        (
            [host] => php.net
            [type] => NS
            [target] => ns2.easydns.com
            [class] => IN
            [ttl] => 10722
        )

)
Additional = Array
(
    [0] => Array
        (
            [host] => pair2.php.net
            [type] => A
            [ip] => 216.92.131.5
            [class] => IN
            [ttl] => 6766
        )

    [1] => Array
        (
            [host] => remote1.easydns.com
            [type] => A
            [ip] => 64.39.29.212
            [class] => IN
            [ttl] => 100384
        )

    [2] => Array
        (
            [host] => remote2.easydns.com
            [type] => A
            [ip] => 212.100.224.80
            [class] => IN
            [ttl] => 81241
        )

    [3] => Array
        (
            [host] => ns1.easydns.com
            [type] => A
            [ip] => 216.220.40.243
            [class] => IN
            [ttl] => 81241
        )

    [4] => Array
        (
            [host] => ns2.easydns.com
            [type] => A
            [ip] => 216.220.40.244
            [class] => IN
            [ttl] => 81241
        )

)

参考

add a note add a note

User Contributed Notes 9 notes

up
25
tobias at herkula dot info
9 years ago
This method has no error handling, it simply puts out "false" and it is impossible to check for NXDOMAIN, SERVFAIL, TIMEOUT or any other error...
up
10
NaturalBornCamper
6 years ago
You might have the same problem as me, where testing a non-existent domain will search for a subdomain relative to the domain you are executing from, for example:

// Test with working domain
var_dump( dns_get_record('google.com', DNS_A) );
/* works, returns
Array
(
    [host] => google.com
    [class] => IN
    [ttl] => 299
    [type] => A
    [ip] => 172.217.12.142
)
*/

// Test with invalid domain on our website (example.com)
var_dump( dns_get_record('invalidtestingname.com', DNS_A) );
/* Doesn't work, pretend it's a subdomain
Array
(
    [host] => invalidtestingname.com.example.com
    [class] => IN
    [ttl] => 299
    [type] => A
    [ip] => xxx.xxx.xxx.xxx
)
*/

If anyone has that problem, add a "dot" at the end of the domain name, for example, instead of
dns_get_record('invalidtestingname.com', DNS_A);
Do this:
dns_get_record('invalidtestingname.com.', DNS_A);
up
14
dylan at pow7 dot com
15 years ago
Get more than one type at once like this:
<?php
$dnsr
= dns_get_record('php.net', DNS_A + DNS_NS);
print_r($dnsr);
?>

Using DNS_ALL fails on some domains where DNS_ANY works. I noticed the function getting stuck on the DNS_PTR record, which caused it to return FALSE with this error:
PHP Warning:  dns_get_record(): res_nsend() failed in ....

This gets all records except DNS_PTR:
<?php
$dnsr
= dns_get_record('php.net', DNS_ALL - DNS_PTR);
print_r($dnsr);
?>
up
12
PHP Joe
10 years ago
Although this works very well for general DNS queries if you want to do a direct DNS query to a specified DNS server (rather than using OS resolution) try PHPDNS: http://www.purplepixie.org/phpdns/

You can do direct (TCP or UDP) low-level queries to a nameserver and recurse at will. Very useful for testing specific servers and also for walking through a recursive resolution.
up
2
heinjan at eendrachtstraat dot nl
7 years ago
Please note that Firewalls and anti malware software detects  (and depending on company policies even blocks) DNS_ANY requests.
In that case the usage of this function fails.
This is because DNS_ANY requests can be  exploited for creating "amplification (D)DOS attackes": You send 1 DNS_ANY request and get a huge amount of information back, thus even small requests can result into hugh network load.

I advise to use a more explicit name-request instead of using DNS_ANY.
up
1
ohcc at 163 dot com
6 years ago
When I use DNS_ALL as the second parameter to invoke dns_get_record() on the OS of Windows, PHP emits a warning with the message "Warning: dns_get_record(): Type '251721779' not supported in blah.php on line blah", and DNS_ANY is always OKAY.
up
0
bohwaz
1 year ago
Sadly this method does not allow for using an arbitrary nameserver.

If you need to make a request  using a specific DNS server, you'll need to use either Pear/Net_DNS2, or libdns ( https://github.com/DaveRandom/LibDNS ).

If you want something shorter and lighter, you can also use this 150 lines function: https://gist.github.com/bohwaz/ddc61c4f7e031c3221a89981e70b830c
up
-9
karl at influ dot io
11 years ago
There's a comment below from 7 years ago regarding BSD and MacOSX, I'd just like to follow up incase some people see that and don't think it'll work on MacOSX.

Software:

    System Software Overview:

      System Version: OS X 10.8.3 (12D78)
      Kernel Version: Darwin 12.3.0
      Boot Volume: Macintosh HD
      Boot Mode: Normal
      Computer Name: Karl’s iMac
      User Name: Karl Kloppenborg (karl)
      Secure Virtual Memory: Enabled
      Time since boot: 10 days 20:24
--------------

# php -a
php > print_r(dns_get_record('google.com', DNS_MX));

Array
(
    [0] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 10
            [target] => aspmx.l.google.com
            [class] => IN
            [ttl] => 749
        )

    [1] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 30
            [target] => alt2.aspmx.l.google.com
            [class] => IN
            [ttl] => 749
        )

    [2] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 50
            [target] => alt4.aspmx.l.google.com
            [class] => IN
            [ttl] => 749
        )

    [3] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 40
            [target] => alt3.aspmx.l.google.com
            [class] => IN
            [ttl] => 749
        )

    [4] => Array
        (
            [host] => google.com
            [type] => MX
            [pri] => 20
            [target] => alt1.aspmx.l.google.com
            [class] => IN
            [ttl] => 749
        )

)
up
-17
Emil
6 years ago
Hi,
If I added domain to directadmin where is script with dns_get_record it gives me local values even if that domain is not really delegated yet to this server.. why?
To Top