標籤:style blog http color 使用 os io 資料
網站品質不錯的網站可以在百度站長平台/資料提交/sitemap欄目下看到即時推送的功能, 目前這個工具是邀請開放, 百度的即時推送的api介面可以即時推送我們新發布的文章, 保證百度在第一時間收錄.
百度站長平台 http://zhanzhang.baidu.com/
開啟百度站長平台, 點開即時推送的添加新資料介面獲得帶token的api推送地址:
http://ping.baidu.com/sitemap?site=www.yourdomain.com&resource_name=sitemap&access_token=xxxxxxx
分享一段網友寫的php即時推送代碼: php 即時推送新發布的文章
fsocketopen方式推送sitemap
1 function sitemap_ping_baidu($urls){ 2 $baidu_ping_url = ‘ping.baidu.com‘; 3 $get = ‘/sitemap?site=www.yourdomain.com&resource_name=sitemap&access_token=xxxxxxx‘; 4 $port=80; 5 if ( ( $io = fsockopen( $baidu_ping_url, $port, $errno, $errstr, 50 ) ) !== false ) { 6 $send = "POST $get HTTP/1.1"."rn"; 7 $send .= ‘Accept: */*‘."rn"; 8 $send .= ‘Cache-Control: no-cache‘."rn"; 9 10 $send .= ‘Host: ‘.$baidu_ping_url."rn";11 $send .= ‘Pragma: no-cache‘."rn";12 //$send .= "Referer: http://".$url.$get."rn";13 //$send .= ‘User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)‘."rn";14 15 $xml = ‘<?xml version="1.0" encoding="UTF-8"?><urlset>‘;16 foreach($urls as $url){17 $xml .= ‘<url>‘;18 $xml .= ‘<loc><![CDATA[‘.$url.‘]]></loc>‘;19 $xml .= ‘<lastmod>‘.date(‘Y-m-d‘).‘</lastmod>‘;20 $xml .= ‘<changefreq>monthly</changefreq>‘;21 $xml .= ‘<priority>0.8</priority>‘;22 $xml .= ‘</url>‘;23 }24 $xml .= ‘</urlset>‘;25 26 $send .= ‘Content-Length:‘.strlen($xml)."rn";27 $send .= "Connection: Closernrn";28 29 $send .= $xml."rn";30 31 fputs ( $io, $send );32 33 $return = ‘‘;34 while ( ! feof ( $io ) )35 {36 $return .= fread ( $io, 4096 );37 }38 return $return;39 }else{40 return false;41 }42 }43 $return = sitemap_ping_baidu(array(‘http://www.yourdomain.com/a.php?id=1‘));
推送後百度會返回的xml文檔
1 <?xml version="1.0" encoding="UTF-8"?> 2 <methodResponse> 3 <params> 4 <param> 5 <value> 6 <int>200</int> 7 </value> 8 <param> 9 </params> 10 </methodResponse>
狀態代碼含義如下
反饋碼 說明
| 200 |
無使用方式錯誤,需要進一步觀察返回的內容是否正確 |
| 400 |
必選參數未提供 |
| 405 |
不支援的請求方式,我們只支援POST方式提交資料 |
| 411 |
HTTP頭中缺少Content-Length欄位 |
| 413 |
推送的資料過大,超過了10MB的限制 |
| 422 |
HTTP頭中Content-Length聲明的長度和實際發送的資料長度不一致 |
| 500 |
站長平台伺服器內部錯誤 |