Home > Backend Development > PHP Tutorial > Call Sina api based on IP to get the city name and convert it into pinyin_PHP tutorial

Call Sina api based on IP to get the city name and convert it into pinyin_PHP tutorial

WBOY
Release: 2016-07-13 10:37:13
Original
828 people have browsed it

Function:

1. Get the current IP address.
2. Call Sina API to get the current city.
3. Convert Chinese to Pinyin and jump.

Copy code The code is as follows:

include './pinyin.php';

//Get the 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 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 through the current ip
$getLocation = getLocation($currentIP);
$currentInfo = json_decode($getLocation, true);

//Determine whether the ip 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 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;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736788.htmlTechArticleFunction: 1. Get the current IP address, 2. Call Sina API to get the current city. 3. Convert Chinese to Pinyin and then jump. Copy the code The code is as follows: ?php include './pinyin.php'...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template