1、登陆百度站长平台,获取到网站的推送接口
2、在网站根目录下新建一个文件夹,命名为“api”,并在api文件夹下创建一个baiduapi.php文件,代码如下(数据库配置请自行添加)
<?php
header('Content-type:text/html;charset=utf-8');
define('DB_HOST','localhost');
define('DB_USER','替换成数据库用户');
define('DB_PASSWORD','替换成数据库密码');
define('DB_DATABASE','替换成数据库名');
define('DB_PORT','3306');
function connect($host=DB_HOST,$user=DB_USER,$password=DB_PASSWORD,$database=DB_DATABASE,$port=DB_PORT){
$link=@mysqli_connect($host,$user,$password,$database,$port);
if(mysqli_connect_errno()){
exit(mysqli_connect_error());
}
mysqli_set_charset($link,'utf8');
return $link;
}
function execute($link,$query){
$result=mysqli_query($link,$query);
if(mysqli_errno($link)){
exit(mysqli_error($link));
}
return $result;
}
$start = date('Y-m-d 00:00:00');
$end = date('Y-m-d H:i:s');
$link=connect();
$query="SELECT * FROM `phome_ecms_news` WHERE `truetime` >= unix_timestamp( '$start' ) AND `truetime` <= unix_timestamp( '$end' )"; //获取当天发表的数据
$result=execute($link,$query);
while($data_list=mysqli_fetch_assoc($result)){
$urls[]="https://www.miyuwu.com".$data_list['titleurl']."\n";//替换成自己的域名
}
$api = 'http://data.zz.baidu.com/urls?site=https://www.miyuwu.com&token=这里要替换成主动提交的密钥';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
echo $result;
?>
3、访问网址baiduapi.php的url,返回成功代码,remain表示今日还可提交的url数量,success表示成功提交的url数量