Call Sina api based on the ip address to obtain the city name and convert it to pinyin. Function: 1. obtain the current IP address. 2. call Sina API to obtain the current city. 3. convert Chinese to pinyin and then jump. Copy the code as follows :? Phpinclude. pinyin. php functions:
1. get the current IP address,
2. call Sina API to obtain the current city.
3. convert Chinese to pinyin and then jump.
The code is as follows:
Include './pinyin. php ';
// Obtain the current ip address
Function getIp (){
$ Onlineip = '';
If (getenv ('http _ CLIENT_IP ') & strcasecmp (getenv ('http _ CLIENT_IP'), 'Unknown ')){
$ Onlineip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ') & strcasecmp (getenv ('http _ X_FORWARDED_FOR'), 'Unknown ')){
$ Onlineip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'Unknown ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], 'Unknown ')){
$ Onlineip = $ _ SERVER ['remote _ ADDR '];
}
Return $ onlineip;
}
// Obtain the city information api
Function getLocation ($ ip ){
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, "http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = json & ip = ". $ ip );
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl, CURLOPT_CONNECTTIMEOUT, 30 );
$ Str = curl_exec ($ curl );
Curl_close ($ curl );
Return $ str;
}
// Current IP address
$ CurrentIP = getIp ();
// Obtain information from the current ip address
$ GetLocation = getLocation ($ currentIP );
$ CurrentInfo = json_decode ($ getLocation, true );
// Determine whether the ip address is valid
If ($ currentInfo ['ret '] ='-1 ')
{
$ CurrentInfo ['city'] = 'unknown ';
}
// Chinese name of the current city
$ CurrentCityName = $ currentInfo ['city'];
$ CurrentCityEName = $ pin-> Pinyin ("$ currentCityName", 'utf8 ');
// City pinyin
Switch ($ currentCityEName)
{
Case 'zhongqing ':
$ CurrentCityEName = 'chungqing ';
Break;
Case 'Shanghai ':
$ CurrentCityEName = 'shifang ';
Break;
Case 'chengdou ':
$ CurrentCityEName = 'hangzhou ';
Break;
Case 'yueshan ':
$ CurrentCityEName = 'leshanc ';
Break;
Case 'junxian ':
$ CurrentCityEName = 'xunxian ';
Break;
Case 'shamen ':
$ CurrentCityEName = 'xiamen ';
Break;
Case 'hangsha ':
$ CurrentCityEName = 'changsha ';
Break;
Case 'weili ':
$ CurrentCityEName = 'yuli ';
Break;
Case 'zhaoyang ':
$ CurrentCityEName = 'chunyang ';
Break;
Case 'danxian ':
$ CurrentCityEName = 'shanxian ';
Break;
Default:
$ CurrentCityEName = $ pin-> Pinyin ("$ currentCityName", 'utf8 ');
Break;
}
// Redirect the browser
Header ("Location: http://www.jb51.net ");
Exit;
Listen 1, get the current IP address, 2, call Sina API, get the current city. 3. convert Chinese to pinyin and then jump. The code is as follows :? Php include './pinyin. php '...