使用PHP怎么根据ip调用新浪api获取城市名-创新互联
本篇文章为大家展示了使用PHP怎么根据ip调用新浪api获取城市名,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
公司主营业务:成都做网站、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出嘉定免费做网站回馈大家。功能:
1,获取当前IP地址,
2,调用新浪API,获取到当前城市。
3,将中文转换为拼音后跳转。
复制代码 代码如下:
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: https://www.yisu.com");
exit;
上述内容就是使用PHP怎么根据ip调用新浪api获取城市名,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网站标题:使用PHP怎么根据ip调用新浪api获取城市名-创新互联
本文路径:http://scyanting.com/article/dijsip.html