1

I used symfony 1.4 to create my application.

I'd like to get the IP adress of the current server to put it within soap request

So, how can i get the IP address of the current server?

Cœur
  • 37,241
  • 25
  • 195
  • 267
hbgamra
  • 749
  • 1
  • 6
  • 18
  • This is not a Symfony related question, please check this answers about getting the server IP on PHP: http://stackoverflow.com/questions/5800927/how-to-identify-server-ip-address-in-php – nikoskip Jul 29 '16 at 18:28
  • 1
    It depends if they need to get the IP address using the framework. It is not recommanded to use globals for example – sinhix Aug 26 '16 at 12:03

3 Answers3

3

For most situations, using $_SERVER['SERVER_ADDR']; will work. If that doesn't work you can try $ip = gethostbyname(gethostname());

Kris Peeling
  • 1,025
  • 8
  • 18
1

Premise of the following method: your domain name has only one IP resolution

Using PHP:

gethostbyname($_SERVER['SERVER_NAME'])

$_SERVER['SERVER_NAME']will generally return your domain name (server_name / ServerName is configured in Nginx / Apache server), and then use gethostbyname().


About $_SERVER['SERVER_ADDR'], it often return a LAN IP address (I only have one server, one domain name, no reverse proxy; cloud server).


About gethostname() In the test, it returns the name of the server (host name, not the domain name you use), and then uses gethostbyname(), will return a LAN IP.


More can be used https://checkip.amazonaws.com/ Get the current IP.

wei
  • 11
  • 2
0

If you have access to the $request object and it is a sfWebRequest (typical request from a browser) you can use:

$request->getPathInfoArray()['SERVER_ADDR']
sinhix
  • 878
  • 6
  • 11