I'm trying to get my ip address.
Here is the code, the getClientIp()
method uses a $_SERVER['REMOTE_ADDR']
global variable internally, so $request->getClientIp()
and $_SERVER['REMOTE_ADDR']
are the same.
<?php
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
Http::response('IP address:' . $request->getClientIp())->json();
I have php deployed in docker on my local machine. So I send a request to localhost
http://localhost/api/v1/ip_address
and get a response.
{
"message": "IP address:172.ХХ.Х.Х", // I replaced my ip numbers with x.
"data": []
}
But there is a problem, the ip address that I get is different from the ip address that applications like "get my ip" give me.
You can just open Google and type in the search "find out my ip online" or "what is my ip" and they will give the correct ip, but the ip that I get from php is not.
I think this is due to the fact that I am making a request to my own computer, and not to a remote server. Can anyone explain why this is happening and if I can get around it?
Update: From php I get the internal ip address because it starts with 172.X.X...