IP定位,天气接口

首先获取IP

////获得本地真实IP
function get_onlineip() {
$ip_json = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=myip");
$ip_arr=json_decode(stripslashes($ip_json),1);
if($ip_arr['code']==0)
{
return $ip_arr['data']['ip'];
}

}

这种百度一大堆,但是可能有时候在本地测试没有用,代码放到服务器上就有用了,

通过IP再获取城市
////根据ip获得访客所在地地名
function Get_Ip_From($ip=''){
if(empty($ip)){
$ip = self::get_onlineip();
}
$ip_json=@file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);//根据taobao ip
$ip_arr=json_decode(stripslashes($ip_json),1);
if($ip_arr['code']==0)
{
return $ip_arr;
}
else
{
return false;
}

}

////获取访客操作系统
function Get_Os(){
if(!empty($_SERVER['HTTP_USER_AGENT'])){
$OS = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/win/i',$OS)) {
$OS = 'Windows';
}
elseif (preg_match('/mac/i',$OS)) {
$OS = 'MAC';
}
elseif (preg_match('/linux/i',$OS)) {
$OS = 'Linux';
}
elseif (preg_match('/unix/i',$OS)) {
$OS = 'Unix';
}
elseif (preg_match('/bsd/i',$OS)) {
$OS = 'BSD';
}
else {
$OS = 'Other';
}
return $OS;
}
else{
return "unknow";
}
}

获取天气,但是只能都是3秒访问一次,我的解决方法,把今天这个城市的天气存到数据库中,然后从数据库读取
  //天气,要传入一个城市
public function weather($Position){


$weather =file_get_contents("http://www.sojson.com/open/api/weather/json.shtml?city=$Position");
// dd($weather);
return $ip_arr=json_decode($weather,true);
// return $weather;
}

这是我获取天气的代码,看上去觉得冗余很多,而且这些接口服务器会降低网页的速度


原文地址:https://www.cnblogs.com/wlphp/p/8407277.html