How to get current system FQDN in php-cli?

wormhit picture wormhit · Sep 2, 2014 · Viewed 7.7k times · Source

Is there a way to retrieve current system fully qualified domain name using php-cli?

What I'm looking for is php function to retrieve the same thing that hostname -f is doing:

$ hostname -f  #returns needed: system01.dev.lan

I tried following:

php_uname('n') // returns: system01
gethostname() // returns: system01
gethostbyname(gethostname()) // returns ip address
gethostbyname(gethostname() . '.') // returns system01

$_SERVER dose not exist (because only cli env)

Again, I know I can do this

php -r "var_dump(shell_exec('hostname -f'));"

But for this trivial task, I think php should have built in functionality to retreive fqdn.

Answer

user740521 picture user740521 · May 10, 2018
gethostbyaddr(gethostbyname(gethostname()));

This returns the FQDN for me while everything else just returns localhost or hostname.