你好,我使用这个API:http://ipapi.co/json要获取本地主机(127.0.0.1)中的国家、城市和真实IP地址,工作完美,但是当我将代码放入我的网站时,我的代码不显示客户端的地址,而是显示服务器的地址,我该如何解决这个问题? p>
我可以使用 $_SERVER['REMOTE_ADDR'];
获取客户端 IP 地址,但我需要此 api 来获取国家和城市:http://ipapi.co/json .
Function TTT() {
$ip = false;
$city = false;
$region = false;
$country = false;
$country_code = false;
if($json = @file_get_contents("http://www.geoplugin.net/json.gp")) {
$obj = json_decode($json);
if(isset($obj->geoplugin_request) && $obj->geoplugin_request != false) {
$ip = "IP: ". $obj->geoplugin_request. "</br>";
}
if(isset($obj->geoplugin_city) && $obj->geoplugin_city != false) {
$city = "City: ". $obj->geoplugin_city. "</br>";
}
if(isset($obj->geoplugin_city) && $obj->geoplugin_city != false) {
$region = "Region: ". $obj->geoplugin_city. "</br>";
}
if(isset($obj->geoplugin_countryName) && $obj->geoplugin_countryName != false) {
$country = "Country: ". $obj->geoplugin_countryName. "</br>";
}
if(isset($obj->geoplugin_countryCode) && $obj->geoplugin_countryCode != false) {
$country_code = "Country Code: ". $obj->geoplugin_countryCode. "</br>";
}
return $ip. $city. $region. $country. $country_code;
}
}
echo TTT();
最佳答案
使用http://ipapi.co/json处的API
$obj = json_decode($json);
将此 JSON 放入关联数组中:
{
"ip": "00.00.00.00",
"city": "Atlanta",
"region": "Georgia",
"country": "US",
"postal": "31532",
"latitude": 34.461,
"longitude": -85.9877,
"timezone": "America/New_York"
}
像这样访问城市和地区:
if(isset($obj->city) && $obj->city != false) {
$city = "City: ". $obj->city. "</br>";
}
if(isset($obj->region) && $obj->region != false) {
$region = "Region: ". $obj->region. "</br>";
}
您似乎没有使用最初声明的 API。
关于javascript - PHP:带有API的真实IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42937164/