Call Sina api based on ip address to get city name and convert it to pinyin _ PHP Tutorial

Source: Internet
Author: User
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 '...

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.