More IP access to city information (application of Baidu API)

Source: Internet
Author: User

/**
* 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.