|
網(wǎng)站質(zhì)量不錯(cuò)的網(wǎng)站可以在百度站長(zhǎng)平臺(tái)/數(shù)據(jù)提交/sitemap欄目下看到實(shí)時(shí)推送的功能, 目前這個(gè)工具是邀請(qǐng)開放, 百度的實(shí)時(shí)推送的api接口可以實(shí)時(shí)推送我們新發(fā)布的文章, 保證百度在第一時(shí)間收錄.
百度站長(zhǎng)平臺(tái) http://zhanzhang.baidu.com/
打開百度站長(zhǎng)平臺(tái), 點(diǎn)開實(shí)時(shí)推送的添加新數(shù)據(jù)接口獲得帶token的api推送地址:
http://ping.baidu.com/sitemap?site=www.yourdomain.com&resource_name=sitemap&access_token=xxxxxxx
分享一段網(wǎng)友寫的php實(shí)時(shí)推送代碼:
php 實(shí)時(shí)推送新發(fā)布的文章
socketopen方式推送sitemap
復(fù)制代碼 代碼如下:
function sitemap_ping_baidu($urls){
$baidu_ping_url = 'ping.baidu.com';
$get = '/sitemap?site=www.yourdomain.com&resource_name=sitemap&access_token=xxxxxxx';
$port=80;
if ( ( $io = fsockopen( $baidu_ping_url, $port, $errno, $errstr, 50 ) ) !== false ) {
$send = "POST $get HTTP/1.1"."rn";
$send .= 'Accept: */*'."rn";
$send .= 'Cache-Control: no-cache'."rn";
$send .= 'Host: '.$baidu_ping_url."rn";
$send .= 'Pragma: no-cache'."rn";
//$send .= "Referer: http://".$url.$get."rn";
//$send .= 'User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)'."rn";
$xml = '<?xml version="1.0" encoding="UTF-8"?><urlset>';
foreach($urls as $url){
$xml .= '<url>';
$xml .= '<loc><![CDATA['.$url.']]></loc>';
$xml .= '<lastmod>'.date('Y-m-d').'</lastmod>';
$xml .= '<changefreq>monthly</changefreq>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
}
$xml .= '</urlset>';
$send .= 'Content-Length:'.strlen($xml)."rn";
$send .= "Connection: Closernrn";
$send .= $xml."rn";
fputs ( $io, $send );
$return = '';
while ( ! feof ( $io ) )
{
$return .= fread ( $io, 4096 );
}
return $return;
}else{
return false;
}
}
$return = sitemap_ping_baidu(array('http://www.yourdomain.com/a.php?id=1'));
推送后百度會(huì)返回的xml文檔
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<int>200</int>
</value>
<param>
</params>
</methodResponse>
狀態(tài)碼含義如下
200 | 無(wú)使用方式錯(cuò)誤,需要進(jìn)一步觀察返回的內(nèi)容是否正確 |
400 | 必選參數(shù)未提供 |
405 | 不支持的請(qǐng)求方式,我們只支持POST方式提交數(shù)據(jù) |
411 | HTTP頭中缺少Content-Length字段 |
413 | 推送的數(shù)據(jù)過(guò)大,超過(guò)了10MB的限制 |
422 | HTTP頭中Content-Length聲明的長(zhǎng)度和實(shí)際發(fā)送的數(shù)據(jù)長(zhǎng)度不一致 |
500 | 站長(zhǎng)平臺(tái)服務(wù)器內(nèi)部錯(cuò)誤 |
php技術(shù):百度實(shí)時(shí)推送api接口應(yīng)用示例,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。