$beginToday = date(strtotime(date("Y-m-d",time())));//今天 开始时间 ✔
$beginThisweek = date(strtotime('-1 monday', time()));//本周 开始时间 ✔
$beginLastweek = date(strtotime('-2 monday', time()));//上周 开始时间
$beginThismonth = date(strtotime(date('Y-m', time())));//本月 开始时间
$beginThismonths = date(strtotime('-1 day',$beginThismonth));//上月最后一天 开始时间
$baidu_push_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];//获取URL
//存在记录(每天提交数量有限,解决多次提交的问题)
if($baidu_push = DB::fetch_first("SELECT * FROM ".DB::table('baidu_push')." WHERE url = '$baidu_push_url'")){
$sid = $baidu_push['sid'];//ID
$gxs = $baidu_push['gxs']+1;//更新次数
$tjs = $baidu_push['tjs']+1;//提交次数
$sta = $baidu_push['status'];//状态
if($sta == '1'){//状态等于1 一周提交一次
if($beginThisweek > $baidu_push['dateline']){
$urls_push = array(
$baidu_push_url,
);
$api_push = 'http://data.zz.baidu.com/urls?site='.$_SERVER['HTTP_HOST'].'&token=这里是16位字母和数字组合的字符串';
$ch_push = curl_init();
$options_push = array(
CURLOPT_URL => $api_push,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls_push),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch_push, $options_push);
$result_push = curl_exec($ch_push);
DB::update('baidu_push', ['updatetime' => time(), 'gxs' => $gxs, 'tjs' => $tjs], 'sid = '.$sid);
}
}else{//状态等于0 第二天提交
if($beginToday > $baidu_push['dateline']){
$urls_push = array(
$baidu_push_url,
);
$api_push = 'http://data.zz.baidu.com/urls?site='.$_SERVER['HTTP_HOST'].'&token=这里是16位字母和数字组合的字符串';
$ch_push = curl_init();
$options_push = array(
CURLOPT_URL => $api_push,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls_push),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch_push, $options_push);
$result_push = curl_exec($ch_push);
DB::update('baidu_push', ['updatetime' => time(), 'gxs' => $gxs, 'tjs' => $tjs], 'sid = '.$sid);
}
}
} else {//不存在记录 新增数据
$urls_push = array(
$baidu_push_url,
);
$api_push = 'http://data.zz.baidu.com/urls?site='.$_SERVER['HTTP_HOST'].'&token=这里是16位字母和数字组合的字符串';
$ch_push = curl_init();
$options_push = array(
CURLOPT_URL => $api_push,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls_push),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch_push, $options_push);
$result_push = curl_exec($ch_push);
$result_400 = str_replace('{"error":400,"message":"over quota"}',"400",$result_push);//获取400状态
if($result_400 == '400'){//提交失败状态0
DB::insert('baidu_push', ['url' => $baidu_push_url, 'dateline' => time(), 'updatetime' => time(), 'gxs' => '1', 'tjs' => '0', 'status' => '0']);
} else {//提交成功状态1
DB::insert('baidu_push', ['url' => $baidu_push_url, 'dateline' => time(), 'updatetime' => time(), 'gxs' => '1', 'tjs' => '1', 'status' => '1']);
}
}