Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active March 10, 2026 23:15
Show Gist options
  • Select an option

  • Save masakielastic/524e2f123d7f7fefee43c4f9a28d92cf to your computer and use it in GitHub Desktop.

Select an option

Save masakielastic/524e2f123d7f7fefee43c4f9a28d92cf to your computer and use it in GitHub Desktop.
Minimal PHP parser for HTTPS DNS Resource Record (type 65)
<?php
/*
Minimal HTTPS RR (DNS type 65) parser for PHP.
Demonstrates how to parse HTTPS/SVCB records returned by:
dns_get_record($host, 65, ..., true)
Extracts ALPN values such as "h2" and "h3".
Reference:
RFC 9460 – Service Binding and Parameter Specification via the DNS (SVCB and HTTPS RRs)
*/
$records = dns_get_record('google.com', 65, $authns, $addtl, true);
$data = $records[0]['data'];
$rr = parseHttpsRR($data);
$alpns = parseAlpn($rr['params'][1]); // key 1 = ALPN
var_dump($rr);
var_dump($alpns);
function parseHttpsRR(string $data): array
{
$offset = 0;
$len = strlen($data);
// priority
$priority = unpack('n', substr($data, $offset, 2))[1];
$offset += 2;
// target name
$labelLen = ord($data[$offset]);
$offset++;
if ($labelLen === 0) {
$target = '.';
} else {
$target = substr($data, $offset, $labelLen);
$offset += $labelLen;
}
$params = [];
while ($offset < $len) {
$key = unpack('n', substr($data, $offset, 2))[1];
$offset += 2;
$paramLen = unpack('n', substr($data, $offset, 2))[1];
$offset += 2;
$value = substr($data, $offset, $paramLen);
$offset += $paramLen;
$params[$key] = $value;
}
return [
'priority' => $priority,
'target' => $target,
'params' => $params
];
}
function parseAlpn(string $value): array
{
$offset = 0;
$alpns = [];
while ($offset < strlen($value)) {
$l = ord($value[$offset]);
$offset++;
$alpns[] = substr($value, $offset, $l);
$offset += $l;
}
return $alpns;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment