百度推送
前端代码:
bootstrap框架:
<!-- 模态框(Modal) -->
<div class="modal fade" id="pushList" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 class="modal-title" id="myModalLabel">
提交<span style="color:red;margin-left:10px;font-size:12px">注意: 在运行过程中请勿刷新浏览器</span>
</h4>
</div>
<div class="modal-body">
<div class="input-group">
<span class="input-group-addon">需提交</span>
<input type="text" class="form-control push_count" aria-label="Amount (to the nearest dollar)">
<span class="input-group-addon">条</span>
</div>
<div class="progress" style="margin:10px 0;display:none;">
<div class="push-progress progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="execPush()">开始</button>
<button type="button" class="btn btn-default close-btn" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
js代码
var running = false;
function push(count,all){
$.post("baidus/fa",{},function(res){
if(res.remain){
if(count > all){
running = false;
$('.push_count').val('');
// 启用关闭按钮
$('.close-btn').show();
// 关闭进度条
$('.progress').hide();
alert("已全部提交完毕,剩余可用次数 " + res.remain + "次");
}else{
count++;
/*进度条*/
$('.push-progress').css('width', count / all * 100 + '%');
console.log(res);
push(count,all);
}
}else{
running = false;
alert("百度可提交的次数已达上限,请明天再来操作吧");
}
});
}
function execPush(){
if(running) return;
var push_count = $('.push_count').val();
if(push_count && push_count > 0){
running = true;
// 开启进度条
$('.progress').show();
// 禁用关闭按钮
$('.close-btn').hide();
// 开始运行
push(0, push_count);
}else{
alert("提交数必须为数字且大于0");
}
}
后台代码:
public function fa(){
$request=Request::instance();
// 推送信息表
$push_info = Db::name('bai_status')->where('id', '=', 1)->find();
if($push_info){
// 最后推送数据的ID
$last_id = $push_info['bid'];
// 获取下一条需要推送的url
$next_url = Db::name("bai")->where("id", ">", $last_id)->find();
if(empty($next_url)){
$next_url = Db::name("baidus")->find();
}
// 处理host
$host = strstr($next_url['linkurl'], "/", true);
if(!$host){
$host = $next_url['linkurl'];
}
// 域名
$site = "https://" . $host;
// 推送token
$token='####';
// 推送api地址
$api = "http://#####?site=$site&token=$token";
// 初始化
$ch = curl_init();
// 参数
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $next_url['linkurl'],
CURLOPT_HTTPHEADER => array('Content-Type: text/plain')
);
// 设置参数
curl_setopt_array($ch, $options);
// 发送请求
$result = (array)json_decode(curl_exec($ch));
// 更新数据库
Db::name('baidus_status')->update(array('id' => 1, 'bid' => $next_url['id'], 'btime' => time()));
// 返回数据
return array(
'code' => $result['success'],
'remain' => $result['remain'],
'push_url' => $next_url['linkurl']
);
}else{
return array('code' => '-1','msg' => '数据库信息有误,请联系管理员');
}
}
数据表设计
DROP TABLE IF EXISTS `fa_bai`;
CREATE TABLE `fa_bai` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`linkurl` varchar(100) COLLATE utf8_unicode_ci NOT NULL COMMENT '推送链接',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3994665 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='百度推送链接表';
DROP TABLE IF EXISTS `fa_bai_status`;
CREATE TABLE `fa_bai_status` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bid` int(11) DEFAULT NULL COMMENT '上次发送ID',
`bnum` int(11) DEFAULT NULL COMMENT '上次发送条数',
`btime` int(11) DEFAULT NULL COMMENT '上次发送时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='发送记录';