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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值