badcow / dns-parser
v1.7.1
2019-02-23 06:22 UTC
Requires
- php: ~7.1
- badcow/dns: 1.3 - 2.1
Requires (Dev)
- phpunit/phpunit: ~7.5
README
DNS解析器已被集成到Badcow DNS区域库中,以实现更好的发布周期。命名空间保持不变。您只需在composer配置中包含badcow/dns
即可。
Badcow DNS区域解析器
此库解析DNS区域文件并输出DNS对象(见Badcow DNS区域库)
构建状态
用法
$file = file_get_contents('/path/to/example.com.txt'); $zone = Badcow\DNS\Parser\Parser::parse('example.com.', $file);
很简单。
安装
使用composer...
"require": { "badcow/dns-parser": "~1.0" }
示例
BIND记录
$ORIGIN example.com.
$TTL 3600
@ IN SOA (
example.com. ; MNAME
post.example.com. ; RNAME
2014110501 ; SERIAL
3600 ; REFRESH
14400 ; RETRY
604800 ; EXPIRE
3600 ; MINIMUM
)
; NS RECORDS
@ NS ns1.nameserver.com.
@ NS ns2.nameserver.com.
info TXT "This is some additional \"information\""
; A RECORDS
sub.domain A 192.168.1.42 ; This is a local ip.
; AAAA RECORDS
ipv6.domain AAAA ::1 ; This is an IPv6 domain.
; MX RECORDS
@ MX 10 mail-gw1.example.net.
@ MX 20 mail-gw2.example.net.
@ MX 30 mail-gw3.example.net.
mail IN TXT "THIS IS SOME TEXT; WITH A SEMICOLON"
处理记录
<?php require_once '/path/to/vendor/autoload.php'; $file = file_get_contents('/path/to/example.com.txt'); $zone = Badcow\DNS\Parser\Parser::parse('example.com.', $file); $zone->getName(); //Returns example.com. foreach ($zone->getResourceRecords() as $record) { $record->getName(); $record->getClass(); $record->getTtl(); $record->getRdata()->output(); }
使用自定义RData处理器
默认情况下,库将处理大多数常见RData类型。偶尔,您可能会遇到不支持的类型。您可以添加自己的RData处理器方法来处理记录类型。例如,您可能想要支持非标准的SPF
记录类型,并返回一个TXT
实例。
$spf = function (\ArrayIterator $iterator): Badcow\DNS\Rdata\TXT { $string = ''; while ($iterator->valid()) { $string .= $iterator->current() . ' '; $iterator->next(); } $string = trim($string, ' "'); //Remove whitespace and quotes $spf = new Badcow\DNS\Rdata\TXT; $spf->setText($string); return $spf; }; $customHandlers = ['SPF' => $spf]; $record = 'example.com. 7200 IN SPF "v=spf1 a mx ip4:69.64.153.131 include:_spf.google.com ~all"'; $parser = new \Badcow\DNS\Parser\Parser($customHandlers); $zone = $parser->makeZone('example.com.', $record);
如果您愿意,也可以覆盖默认处理器,只要您的处理器方法返回Badcow\DNS\Rdata\RdataInterface
实例。