This article describes how to call Sina api Based on ip addresses to obtain city names and convert them to pinyin. For more information, see the following 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.php.net ");
Exit;