熊掌号的添加推送和周级推送有手动推送和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),新增内容和历史内容推送各运行一次即可。显示如下,即推送成功。
发表评论