How do I find my server's IP address in PHP(CLI)
You can get the hostname by using gethostname
try this it should return the ip address of the server
$host= gethostname();
$ip = gethostbyname($host);
If you are working with PHP < 5.3, this may help (on *NIX based systems atleast):
mscharley@S04:~$ cat test.php
#!/usr/bin/env php
<?php
function getIPs($withV6 = true) {
preg_match_all('/inet'.($withV6 ? '6?' : '').' addr: ?([^ ]+)/', `ifconfig`, $ips);
return $ips[1];
}
$ips = getIPs();
var_dump($ips);
mscharley@S04:~$ ./test.php
array(5) {
[0]=>
string(13) "72.67.113.141"
[1]=>
string(27) "fe80::21c:c0ff:fe4a:d09d/64"
[2]=>
string(13) "72.67.113.140"
[3]=>
string(9) "127.0.0.1"
[4]=>
string(7) "::1/128"
}
mscharley@S04:~$
Or, if you don't anticipate doing it often, then perhaps this would work (just don't abuse it):
$ip = file_get_contents('http://whatismyip.org/');