很久前,織夢dedecms自己有一套主動ping百度的插件,但是后來好像有幾年用不了了 。
最近百度推出了實時推送鏈接地址給百度,這樣肯定比sitemap方便多了.而且也可以確保文章的原創性。
不知道為什么我在網上還沒看到織夢有這樣的教程,所以我就根據百度接口寫了個教程出來,給各位織夢愛好者看看 。
關于織夢的百度實時推送我寫了兩種方法,大家可以自行選擇 :
1、
手動創建一個文件,每天訪問這個文件就可以把當天的全部文章推送到百度搜索引擎.
在根目錄下面創建一個fcz8k.php 訪問后會返回百度接口結果
代碼如下
<?php
require_once ("include/common.inc.php");
require_once "include/arc.partview.class.php";
require_once('include/charset.func.php');
$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);//當天結束時間戳
$query = "SELECT arch.id,types.typedir FROM dede_arctype as types inner join dede_archives as arch on types.id=arch.typeid where pubdate<".$dayEnd." AND pubdate>".$dayBegin.""; //這里dede換成你們自己的表前綴
$urls="";
$dsql->Execute('arch.id,types.typedir',$query);
while($row = $dsql->GetArray('arch.id,types.typedir'))
{
$urls.="http://fcz8k.com".str_replace("{cmspath}","",$row['typedir'])."/".$row[id].".html".",";
//將上邊的http://fcz8k.com換成你的網址
}
$urls=substr($urls,0,-1);
$urls = explode(",",$urls);
$api = 'http://data.zz.baidu.com/urls?site=youwujun.com.cn&token=xxxxx'; // 前邊的site換成自己的site xxx換成自己的密鑰
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode(" ", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
echo $result.count($urls);
?>
百度接口返回說明:
推送成功
狀態碼為200,可能返回以下字段:
字段
是否必選
參數類型
說明
success
是
int
成功推送的url條數
remain
是
int
當天剩余的可推送url條數
not_same_site
否
array
由于不是本站url而未處理的url列表
not_valid
否
array
不合法的url列表
成功返回示例:
{
"remain":4999998,
"success":2,
"not_same_site":[],
"not_valid":[]
}
推送失敗
狀態碼為4xx,返回字段有:
字段
是否必傳
類型
說明
error
是
int
錯誤碼,與狀態碼相同
message
是
string
錯誤描述
失敗返回示例:
{
"error":401,
"message":"token is not valid"
}
2、
第二種是發布一篇文章,就像百度推送一次,這種比較方便,我就是用這種
打開織夢后臺的 article_add.php 文件.找到差不多262行的樣子
注意:如果你系統設置的-》核心選項
,如果是否直接
加入以下代碼,否則 注意下面的提示
$urls="http://www.youwujun.com.cn".$artUrl;//前面域名換成你自己的 如果上面圖片選擇的是是 就把"http://www.youwujun.com.cn". 去掉
$urls = explode(",",$urls);
$api = 'http://data.zz.baidu.com/urls?site=www.youwujun.com.cn&token=xxxx'; // 前邊site換成自己的site xxx換成自己的密鑰
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode(" ", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
就OK了 ,如果想看添加成功沒,可以在修改下面一兩行的樣子的代碼
請選擇你的后續操作".$result.$urls[0].":
result是看百度返回的結果,urls是看你推送的url.
基本上就OK了,如果你想讓修改文章的時候也事實推送,就類似我上面一樣去修改article_edit.php就好了.