/**
* Get city based on IP
* @param string $IP IP address
* @return Array
* http://api.map.baidu.com/location/ip?ak=32f38c9491f2da9eb61106aaab1e9739&ip= "+ip
*/
Public Function Getcitybyip ($IP)
{
$result = false;
$url = "http://api.map.baidu.com/location/ip?ak=32f38c9491f2da9eb61106aaab1e9739&ip=". $ip;
$result = file_get_contents ($url);
$result = Json_decode ($result);
$result = Self::objtoarr ($result);
if (Is_array ($result) && isset ($result [' status ')]) {
if ($result [' status '] = = 0 && isset ($result [' content ']) {
$result [' city '] = iconv (' utf-8 ', ' GBK ', $result [' content '] [' address_detail '] [' City '];
}
}
return $result;
}
/**
* Convert object type data to array form
* @param stdClass Object type data $obj
* @return Array
*/
public static function Objtoarr ($obj)
{
$ret = Array ();
foreach ($obj as $key = = $value) {
if (GetType ($value) = = ' Array ' | | GetType ($value) = = ' object ') {
$ret [$key] = Self::objtoarr ($value);
} else {
$ret [$key] = $value;
}
}
return $ret;
}
/**
* Get client IP Address
* @param Boolean $pasportlogin Whether it is a Passport login
* @param boolean $forwarded whether to fetch http_x_forwarded_for
* @return String
*/
function Get_client_ip ($pasportlogin = true, $forwarded = False)
{
if (getenv ("Http_x_real_ip") && strcasecmp (getenv ("Http_x_real_ip"), "Unknown") &&! $forwarded)
$ip = getenv ("Http_x_real_ip");
else if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "Unknown")
$ip = getenv ("Http_client_ip");
else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "Unknown")
$ip = getenv ("Http_x_forwarded_for");
else if (getenv ("REMOTE_ADDR") && strcasecmp (getenv ("REMOTE_ADDR"), "Unknown")
$ip = getenv ("REMOTE_ADDR");
else if (isset ($_server[' remote_addr ')) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], "unknown"))
$ip = $_server[' remote_addr ');
Else
$ip = "Unknown";
if ($pasportlogin = = = True) {
if (False!== Strpos ($ip, ', ')) {
$arrIp = Explode (', ', $IP);
for ($i = 0; $i < count ($arrIp); $i + +) {
if (!eregi ("^ (10|172\.16|192\.168) \.", $arrIp [$i])) {
$ip = $arrIp [$i];
Break
}
}
}
}
return ($IP);
}
More IP access to city information (application of Baidu API)