PHP dns_get_record missing records for subdomains
I’m trying to get a complete list of DNS records associated with a domain, in PHP, if that’s possible.
I have been searching for a while, and most of the answers to related questions show code like this:
However, when I use that code to check one of my domains, there are a lot of records missing. No records are shown at all for subdomains.
It is possible to get a complete list of DNS records for a domain along with the records for any subdomains, in PHP, without knowing ahead of time what those subdomains are?
1 Answer 1
As I posted, I would suggest using Pear Net DNS2. Here is a thorough example: http://pastebin.com/bAriPVN9
When you do a lookup, I prefer to check against Google Public DNS (8.8.8.8), but you may have another preference. Here is an example of how you can set that:
My original code was being setup to work like an API. Users could make a GET or POST request and get Result Records in different ways. This project did not go in that direction, yet you will see some extra steps in the code toward that. Next you want to set or pick the Record Types you’re going to want to lookup.
I wanted A Records, Mail Exchange Records, Service Records, Text, Conical Records, and Name Server Records. I never tested with lower case, but had no interest in finding out if either worked.
I then went through and picked all the Sub-domains I wanted to lookup. Since I was potentially working with different Name Servers, I didn’t want to leave it to chance. If you ask for ALL or *, the NS may ignore the request. Ask for a specific record, it will always give you a response.
Now we can start iterating all this and collecting the results. Results can come back in different ways depending on the type of record or the Resource Data elements. Also if the lookup fails, maybe the record does not exist, we have to handle that. Hence the try<> catch() blocks.
MX And SRV Records can return lots of different data. For example, a domain may have 4 MX Records. So you will get an Array of Objects returned that you have to sift through. I think I copy/pasted that code block twice by accident.
Sure, it may be more effort yet you get the right results. I hope this helps get you going.
PHP | функция dns_get_record ()
Функция dns_get_record () — это встроенная функция в PHP, которая возвращает записи ресурсов DNS для указанного имени хоста в Интернете.
Синтаксис:
Параметр: эта функция принимает пять параметров, как указано выше и описано ниже:
Возвращаемое значение:
Примечание. Эта функция доступна для PHP 5.0.0 и более поздних версий.
Пример 1:
Выход:
Array ( [0] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 1 [target] => aspmx.l.google.com ) [1] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 10 [target] => alt3.aspmx.l.google.com ) [2] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 10 [target] => alt4.aspmx.l.google.com ) [3] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 5 [target] => alt1.aspmx.l.google.com ) [4] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 5 [target] => alt2.aspmx.l.google.com ) )
Пример 2: систематический вывод
Выход:
Пример 3: все возможные значения типа $
Dns get record php
Description array dns_get_record ( string hostname [, int type [, array &authns, array &addtl]] )
This function returns an array of associative arrays. Each associative array contains at minimum the following keys:
Таблица 1. Basic DNS attributes
hostname should be a valid DNS hostname such as «www.example.com». Reverse lookups can be generated using in-addr.arpa notation, but gethostbyaddr() is more suitable for the majority of reverse lookups.
Замечание: Because of eccentricities in the performance of libresolv between platforms, DNS_ANY will not always return every record, the slower DNS_ALL will collect all records more reliably.
Таблица 2. Other keys in associative arrays dependant on ‘type’
Пример 1. Using dns_get_record()
Produces output similar to the following:
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 ) )
Since it’s very common to want the IP address of a mail server once the MX record has been resolved, dns_get_record() also returns an array in addtl which contains associate records. authns is returned as well containing a list of authoritative name servers.
Пример 2. Using dns_get_record() and DNS_ANY
Produces output similar to the following:
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 ) )
Если Вы не нашли что искали, то рекомендую воспользоваться поиском по сайту:
getmxrr
(PHP 4, PHP 5, PHP 7, PHP 8)
getmxrr — Получает записи MX, соответствующие переданному доменному имени хоста
Описание
Список параметров
Возвращаемые значения
Примечания
Для совместимости с более ранними версиями PHP под Windows, где не было реализации этой функции, используйте класс » PEAR » Net_DNS.
Смотрите также
User Contributed Notes 19 notes
For some reason this and the other DNS lookup functions seem to be really slow on my Linux box. I’ve checked several things and have no explanation.
As a work-around, I gave in and just used a system call to dig:
( «fakedomain.org» );
CheckMX ( «hotmail.com» );
MX record found for fakedomain.org not found!
MX Record for hotmail.com found
As someone else pointed out, it is prudent to check to see if the TLD has an IP address if the MX record is not found.
I tried using getmxrr() to validate the domain portion of email addresses in enquiry submission forms, and there is a curious effect with some top-level domains when checking non-existant domains.
With sdlkfjsdl.com, since the domain does not exist, getmxrr() returns false, as expected, and the returned mxhosts array is empty.
But with sdlkfjsdl.gov, getmxrr() returns true, and the returned mxhosts array contains one element: NULL
With sdlkfjsdl.org, getmxrr() returns true, and the returned mxhosts array contains one element: ‘0.0.0.0’
With sdlkfjsdl.co.uk, getmxrr() returns true and supplies one MX record: uk-net-wildcard-null-mx.centralnic.net
Big thanks to the rfc community.
?>
/* Exampe of use: */
$mx = new mxlookup ( «php.net» );
dns_get_record
dns_get_record — Fetch DNS Resource Records associated with a hostname
Описание
Список параметров
hostname should be a valid DNS hostname such as «www.example.com». Reverse lookups can be generated using in-addr.arpa notation, but gethostbyaddr() is more suitable for the majority of reverse lookups.
Замечание: Per DNS standards, email addresses are given in user.host format (for example: hostmaster.example.com as opposed to hostmaster@example.com), be sure to check this value and modify if necessary before using it with a functions such as mail().
Замечание: Because of eccentricities in the performance of libresolv between platforms, DNS_ANY will not always return every record, the slower DNS_ALL will collect all records more reliably.
Passed by reference and, if given, will be populated with Resource Records for the Authoritative Name Servers.
Passed by reference and, if given, will be populated with any Additional Records.
Возвращаемые значения
This function returns an array of associative arrays. Each associative array contains at minimum the following keys:
| Attribute | Meaning |
|---|---|
| host | The record in the DNS namespace to which the rest of the associated data refers. |
| class | dns_get_record() only returns Internet class records and as such this parameter will always return IN. |
| type | String containing the record type. Additional attributes will also be contained in the resulting array dependant on the value of type. See table below. |
| ttl | Time To Live remaining for this record. This will not equal the record’s original ttl, but will rather equal the original ttl minus whatever length of time has passed since the authoritative name server was queried. |
Примеры
Пример #1 Using dns_get_record()
