I’m getting an error with this code:
$type = "DNS_" . strtoupper($_GET['type']);
$array = dns_get_record($domain, $type);
Helpful info:
$_GET['type'] = "a"
If I enter DNS_A
in dns_get_record
, the code works. But when entering $type
, I get an error.
The type is dynamic and I get it from a URL parameter, which is why I can’t hard code it in.
What am I doing wrong?
3
Answers
DNS_A actually means integer 1, NOT a string "DNS_A"
So please use
or use $_GET[‘type’] = 1 and then use
Please see official documentation: ( int $type = DNS_ANY …. and so on)
https://www.php.net/manual/en/function.dns-get-record.php
Note: you can mix the $type like:
Keys (pre-defined constants)
As advised by @bilbodog, there is also DNS_CAA:
dns_get_record expects second parameter to be Integer. In yoru case it is a string.
Please replace following numbers based on your required records:
List of DNS Record Types
Parameter 2 has to be one of these predefined constants
so you will need to get the value of the constant in order to make your code flexible like that.
You were passing a string so use
constant()
to convert that string into the integer value associated with that constantExample
RESULT