在之前,織夢有一套自己主動ping百度的插件,但是後來用不了了,最後百度推出了實時推送鏈接地址到百度,這樣要比sitemap方便好用多了,而且還能保證文章的原創,下面我通過查閱相關資料,根據百度的接口寫了一篇sitemap地圖實時推送給百度的處理辦法,分享給大家。
關於織夢的百度實時推送我寫了兩種方法,大家可以自行選擇:
1.手動創建一個文件,每天訪問這個文件就可以把當天的全部文章推送到百度搜索引擎. 在根目錄下面創建一個tuisong.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://www.baidu.com".str_replace("{cmspath}","",$row['typedir'])."/".$row[id].".html".",";
//將上邊的http://baidub.com換成你的網址
}
$urls=substr($urls,0,-1);
$urls = explode(",",$urls);
$api = 'http://data.zz.baidu.com/urls?site=www.baidu.com&token=hereistoken'; // 前邊的site換成自己的site xxx換成自己的密鑰
$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.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.baidu.com".$artUrl;//前面域名換成你自己的 如果上面圖片選擇的是是 就把"http://baidu.com". 去掉
$urls = explode(",",$urls);
$api = 'http://data.zz.baidu.com/urls?site=www.0cx.cc&token=hereistoken'; // 前邊site換成自己的site xxx換成自己的密鑰
$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);
就OK了 ,如果想看添加成功沒,可以在修改下面一兩行的樣子的代碼
復制代碼 代碼如下:
請選擇你的後續操作".$result.$urls[0].":
result是看百度返回的結果,urls是看你推送的url.
基本上就OK了,如果你想讓修改文章的時候也事實推送,就類似我上面一樣去修改article_edit.php就好了.
以上就是本文的全部內容,希望大家喜歡。