共计 3164 个字符,预计需要花费 8 分钟才能阅读完成。
熊掌号 的添加推送和周级推送有手动推送和 api 自动推送两种方式,我们这里介绍一下使用PHP链接数据库,从数据库中直接寻找今日的文章自动推送给 百度 熊掌号
新增内容接口调用地址
http://data.zz.baidu.com/urls?appid=1601114643500123&token=XYV3Ewy6kgS6dlbb&type=realtime
历史内容接口调用地址:
http://data.zz.baidu.com/urls?appid=1601114643500123&token=XYV3Ewy6kgS6dlbb&type=batch
对比发现,只有链接后面的 type 的赋值不一样,其他地方都是一样的,因此我们只说一种 API 新增内容接口调用方法就可以了。
API 提交提供了四种推送示例:curl 推送、post 推送、php推送、ruby 推送。
四种推送方式的原理都是一样的
第一步:获取到网站链接(最好是当天的链接)
第二步:调用 API 接口
第三步:数据推送给百度
这里官方提供了 PHP 的推送代码 我们从上面进行改造 新建一个 baidu.php
<?php
$urls = array(
'http://www.hnzzwz.com/1.html',
'http://www.hnzzwz.com/2.html',
);
$api = 'http://data.zz.baidu.com/urls?appid=1602764643500857&token=XYV3Ewi6kgS6dlYb&type=realtime';
$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;
?>
改造PHP代码,将数据库代码插入进去
$con=mysql_connect('localhost','root','root') or die('失败'.mysql_error());
mysql_select_db(DB_NAME,$con) or die('选择数据库失败'.mysql_error());
获取当前时间
$year = date("Y");
$month = date("m");
$day = date("d");
$dayBegin = mktime(0,0,0,$month,$day,$year);
$dayEnd = mktime(23,59,59,$month,$day,$year);
sql 语句
$query = "SELECT arch.id,types.typedir FROM DB_NAME as types inner join DB_NAME as arch on types.id=arch.typeid where pubdate<".$dayEnd." AND pubdate>".$dayBegin."";
将获取到的数据放入 PHP
$urls="";
$dsql->Execute('arch.id,types.typedir',$query);
while($row = $dsql->GetArray('arch.id,types.typedir'))
{$urls.="http://".$_SERVER['SERVER_NAME'].str_replace("{cmspath}","",$row['typedir'])."/".$row[id].".html".",";
}
$urls=substr($urls,0,-1);
$urls = explode(",",$urls);
每天添加文章后,执行该 php 代码,之后自动提交百度熊掌号,这里改造的时候一定要注意,尽量不要重复提交,以下是 PHPCMS 的提交代码,用 phpcms 的朋友,可以很容易的自动提交
phpcms 分为 新增内容提交 和历史提交 我们分两个文件 放入到 phpcms 的 api 目录中
<?php
//*** 默认文章模型,文章模型 ID 为 1
$moduleId = intval($_GET['mid']);
if(!$moduleId){$moduleId=1;}
$year = date("Y");
$month = date("m");
$day = date("d");$dayBegin = mktime(0,0,0,$month,$day,$year);// 当天开始时间戳 $dayEnd = mktime(23,59,59,$month,$day,$year);// 当天结束时间戳
$contentModel = pc_base::load_model('content_model');
$contentModel->set_model($moduleId);
$contentDatas = $contentModel->select("status=99 AND inputtime BETWEEN $dayBegin AND $dayEnd",'id,url,catid,title,keywords,description,thumb,inputtime','','inputtime DESC,id DESC');
$urls=array();
foreach ($contentDatas as $r) {array_push($urls,$r['url']);
}
if(count($urls)==0){
echo '时间范围内没有可推送的数据';
exit();}
$api = 'http://data.zz.baidu.com/urls?appid=1602339615328938&token=ifrkVxbSFbPAlCnk&type=realtime'; // 前边的熊掌号 ID 和密钥换成自己的
$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;
?>
历史内容推送接口代码只需把上面 当天开始时间戳地方 和前边的熊掌号 ID 和密钥换成自己的
改为下面的代码:
$dayBegin = mktime(0,0,0,7,1,2015);// 当天开始时间戳
和
http://data.zz.baidu.com/urls?appid=1602339615328938&token=ifrkVxbSFbPAlCnk&type=batch
phpcms 使用方法:www. 你的域名.com/api.php?op=baidu(baidu 改成你放在 api 目录里的文件名,不带 php),新增内容和历史内容推送各运行一次即可。显示如下,即推送成功。