您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用PHP怎么根據ip調用新浪api獲取城市名,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
功能:
1,獲取當前IP地址,
2,調用新浪API,獲取到當前城市。
3,將中文轉換為拼音后跳轉。
復制代碼 代碼如下:
<?php
include './pinyin.php';
//獲取當前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;
}
//獲取城市信息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;
}
//當前ip地址
$currentIP = getIp();
//通過當前ip獲取信息
$getLocation = getLocation($currentIP);
$currentInfo = json_decode($getLocation, true);
//判斷ip是否為有效
if($currentInfo['ret'] == '-1')
{
$currentInfo['city'] = 'unknown';
}
//當前城市中文名
$currentCityName = $currentInfo['city'];
$currentCityEName = $pin->Pinyin("$currentCityName",'UTF8');
//城市拼音多音字
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;
}
//重定向瀏覽器
header("Location: http://www.5655pk.com");
exit;
上述內容就是使用PHP怎么根據ip調用新浪api獲取城市名,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。