Call Sina api Based on the ip address to obtain the city name and convert it to the pinyin _ php instance

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

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.