thikphp5.0 ip地址库 解决卡顿问题 curl_init
使用淘宝新浪的地址库非常的使用,但是调用有时候会出现很慢。会导致卡在当前网页。
要想不影响当前速度,因此要使用 curl_init功能。
项目案例:会员登陆日志
user_log 字段:id,user_id,user_name,ip,address,add_time
思路:
1.如果登陆成功send线下发送请求,带参数id,name,ip
2.在方法add_log接收参数,并且开始转化值(要插入user_log字段的数据),其中会用到getAddressFromIp 淘宝新浪地址库

具体操作:
第一步骤:如果登录成功,就开始发送异步请求
$res = db()->where()->update();
if($res){
// 创建session数据
$user['id'] = $login['data']['id'];
$user['name'] = $login['data']['name'];
$user['role_name'] = model('role')->RoleText($login['data']['role_id']);
$user['action_list'] = $login['data']['action_list'];
$user['token'] = $token; Session::set('user',$user);
// 新增登陆日志
$http = [
'id'=>Session::get('user.id'),
'name' => Session::get('user.name'),
'ip' => request()->ip(),
];
send("http://".request()->host().url('login/add_log',$http));
//send('http://localhost/rookie/admin/login/add_log.html');
// 跳转内页
$this->redirect('index/index'); }else{
$this->error('登陆失败');
}
第二步骤,要准备写几个方法,方便后面调用
/**
* php 异步请求数据
* @host 格式
* @http://localhost/rookie/login/add.html
* @http://www.xxx.com
* */
function send($host){
// 创建一个新cURL资源
$ch = curl_init(); // 设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_HEADER, );
// 抓取URL并把它传递给浏览器
curl_exec($ch);
// 关闭cURL资源,并且释放系统资源
curl_close($ch);
}
/**
* 新增登陆日志
* @ get传递过来参数 id,name,ip
* 格式:http://localhost/rookie/admin/login/add_log.html?id=1&name=admin&ip=113.102.134.153
* @ 需要合成 id,name,ip,address,add_time
* */
public function add_log(){
//$ip = '113.102.134.153';
$user_id = input('param.id');
$user_name = input('param.name');
$ip = input('param.ip'); $data['user_id'] = $user_id;
$data['user_name'] = $user_name;
$data['ip'] = $ip;
$address = getAddressFromIp($ip);
$data['address'] = implode(' ',$address);
$data['add_time'] = time(); db('user_login')->insert($data); //file_put_contents('7.txt',urldecode(json_encode($data)));
//file_put_contents('7.txt',$user_name);
}
/*
* 新浪,淘宝IP地址库
* */
function getAddressFromIp($ip){
$urlTaobao = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
$urlSina = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$ip;
$json = file_get_contents($urlTaobao);
$jsonDecode = json_decode($json);
if($jsonDecode->code==){//如果取不到就去取新浪的
$data['country'] = $jsonDecode->data->country;
$data['province'] = $jsonDecode->data->region;
$data['city'] = $jsonDecode->data->city;
$data['isp'] = $jsonDecode->data->isp;
return $data;
}else{
$json = file_get_contents($urlSina);
$jsonDecode = json_decode($json);
$data['country'] = $jsonDecode->country;
$data['province'] = $jsonDecode->province;
$data['city'] = $jsonDecode->city;
$data['isp'] = $jsonDecode->isp;
$data['district'] = $jsonDecode->district;
return $data;
}
}
thikphp5.0 ip地址库 解决卡顿问题 curl_init的更多相关文章
- 解析纯真IP地址库
一周以来,一直在做 IP地址库的解析.从调研到编码到优化,大概花了有七八天的时间.感觉很好玩.总结一下整个做的过程. 1.关于IP 地址库的解析方式 目前主要的解析方式有两种:通过API,或通过IP数 ...
- 用淘宝ip地址库查ip
这是一个通过调用淘宝ip地址库实现ip地址查询的功能类 using System; using System.Collections.Generic; using System.Linq; using ...
- Delphi使用JSON解析调用淘宝IP地址库REST API 示例
淘宝IP地址库:http://ip.taobao.com,里面有REST API 说明. Delphi XE 调试通过,关键代码如下: var IdHTTP: TIdHTTP; RequestURL: ...
- 淘宝IP地址库采集器c#代码
这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下. 最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息.网上的Ip纯真数据库,下载下来一看 ...
- 淘宝IP地址库API接口(PHP)通过ip获取地址信息
淘宝IP地址库网址:http://ip.taobao.com/ 提供的服务包括: 1. 根据用户提供的IP地址,快速查询出该IP地址所在的地理信息和地理相关的信息,包括国家.省.市和运营商. 2. 用 ...
- 淘宝IP地址库采集
作者:阿宝 更新:2016-08-31 来源:彩色世界(https://blog.hz601.org/2016/08/31/taobao-ip-sniffer/index.html) 简述 当初选择做 ...
- 【竞价网站绝技】根据访客ip,页面显示访客所属城市的html代码(借用YY IP地址库)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 淘宝IP地址库API地址
淘宝IP地址库:http://ip.taobao.com/instructions.php 接口说明 1. 请求接口(GET): http://ip.taobao.com/service/getI ...
- XXX全球 IP 地址库
XXX全球 IP 地址库 Bulgaria 93.123.23.1 93.123.23.2 93.123.23.3 93.123.23.4 93.123.23.5 93.123.23.6 93.123 ...
随机推荐
- Centos6.5生成环境配置--nginx1.9 + PHP+可多个tomcat(多个端口)+多域名+java web 负载均衡
安装n p 参考: CentOS6.5搭建LNMP http://www.cnblogs.com/xiaoit/p/3991037.html http://blog.csdn.net/keyunq/a ...
- 改改"坏"代码
程序猿很多时候费了九牛二虎之力使用各种黑科技实现了某个功能,终于可以交差,但整个过程就像个噩梦,一般人是不太愿意回过头去阅读自己写的代码的,交出去的代码就让它如往事般随风吧. 可你不愿读自己的代码,却 ...
- Android——shape和selector和layer-list的(详细说明 转)
<shape>和<selector>在Android UI设计中经常用到.比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到<shape> ...
- Java编程的逻辑 (66) - 理解synchronized
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- NHibernate 知识点整理
1.实体bool类型属性与数据库映射.如实体中有bool属性IsDelete.配置如下 <property column="IsDelete" name="IsDe ...
- maven配置阿里云仓库
在mirrors的节点中添加: <mirror> <!--This sends everything else to /public --> <id>nexus-a ...
- iis7.5 发布mvc出错的解决办法
发布mvc,配置iis7.5时,遇到这个错误. xxxx'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b ...
- linux软件管理(六)
[教程主题]:linux软件管理 [1]软件包管理 在系统管理中,软件包的管理是最重要的,是系统管理的基础的基础, 只有我们学会软件包的管理才能谈得上其它的应用. RPM RPM是软件管理程序,提供软 ...
- rabbitMQ rabbitmq-server -detached rabbitmq-server -detached rabbitmq-server -detached
[root@localhost mnesia]# cat /etc/rabbitmq/rabbitmq-env.conf RABBITMQ_MNESIA_BASE=/home/rabbitmq/mne ...
- 小程序的tab标签实现效果
swiper制作tab切换 index.html ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <view class="swiper-tab&qu ...