Call Sina API based on IP to get the city name and turn it into Pinyin _php tutorial

Source: Internet
Author: User
Function:

1, gets the current IP address,
2, call Sina API, get to the current city.
3, convert Chinese to pinyin after jump.

Copy the Code code as follows:
Include './pinyin.php ';

Get current IP
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;
}

Get 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 ();

Get information from the current IP
$getLocation = GetLocation ($currentIP);
$currentInfo = Json_decode ($getLocation, true);

Determine if the IP is valid
if ($currentInfo [' ret '] = = '-1 ')
{
$currentInfo [' city '] = ' unknown ';
}

Current City Chinese name
$currentCityName = $currentInfo [' City '];
$currentCityEName = $pin->pinyin ("$currentCityName", ' UTF8 ');

City Pinyin Polyphone
Switch ($currentCityEName)
{
Case ' zhongqing ':
$currentCityEName = ' Chongqing ';
Break

Case ' Shenfang ':
$currentCityEName = ' Shifang ';
Break

Case ' Chengdou ':
$currentCityEName = ' Chengdu ';
Break

Case ' Yueshan ':
$currentCityEName = ' Leshan ';
Break

Case ' Junxian ':
$currentCityEName = ' Xunxian ';
Break

Case ' Shamen ':
$currentCityEName = ' Xiamen ';
Break

Case ' Zhangsha ':
$currentCityEName = ' Changsha ';
Break

Case ' Weili ':
$currentCityEName = ' Yuli ';
Break

Case ' Zhaoyang ':
$currentCityEName = ' Chaoyang ';
Break

Case ' Danxian ':
$currentCityEName = ' Shanxian ';
Break

Default
$currentCityEName = $pin->pinyin ("$currentCityName", ' UTF8 ');
Break
}

redirect Browser
Header ("Location:http://www.jb51.net");
Exit

http://www.bkjia.com/PHPjc/736788.html www.bkjia.com true http://www.bkjia.com/PHPjc/736788.html techarticle function: 1, get current IP address, 2, call Sina API, get to current city. 3, convert Chinese to pinyin after jump. Copy the code 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.