代码下载:http://www.zuidaima.com/share/1822672957737984.htm
原文:seo优化之Google和Baidu Ping服务实现快速收录文章的java,php代码实现
最近在做关于google和百度的ping服务,希望能提高搜索引擎蜘蛛的抓取频率,搜索了大半天都不太好用,所以自己写了一份,大家可以参考下:
class="java">package com.zuidaima.core.util; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; public class Ping { public static final String BAIDU_RPC = "http://ping.baidu.com/ping/RPC2"; public static final String GOOGLE_RPC = "http://blogsearch.google.com/ping/RPC2"; private static String buildMethodCall(String title, String url, String shareURL, String rssURL) { StringBuffer buffer = new StringBuffer(); buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); buffer.append("<methodCall>"); buffer.append("<methodName>weblogUpdates.extendedPing</methodName>"); buffer.append("<params>"); buffer.append("<param><value><string>" + url + "</string></value></param>"); buffer.append("<param><value><string>" + title + "</string></value></param>"); buffer.append("<param><value><string>" + shareURL + "</string></value></param>"); buffer.append("<param><value><string>" + rssURL + "</string></value></param>"); buffer.append("</params>"); buffer.append("</methodCall>"); return buffer.toString(); } public static String pingBaidu(String title, String url, String shareURL, String rssURL) throws Exception { PostMethod post = new PostMethod(BAIDU_RPC); post.addRequestHeader("User-Agent", "request"); post.addRequestHeader("Content-Type", "text/xml"); String methodCall = buildMethodCall(title, url, shareURL, rssURL); RequestEntity entity = new StringRequestEntity(methodCall, "text/xml", "utf-8"); post.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); // httpclient.getHostConfiguration().setProxy("127.0.0.1", 8888); httpclient.executeMethod(post); String ret = post.getResponseBodyAsString(); post.releaseConnection(); return ret; } public static String pingGoogle(String title, String url, String shareURL, String rssURL) throws Exception { PostMethod post = new PostMethod(GOOGLE_RPC); post.addRequestHeader("User-Agent", "request"); post.addRequestHeader("Content-Type", "text/xml"); String methodCall = buildMethodCall(title, url, shareURL, rssURL); RequestEntity entity = new StringRequestEntity(methodCall, "text/xml", "utf-8"); post.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); // httpclient.getHostConfiguration().setProxy("127.0.0.1", 8888); httpclient.executeMethod(post); String ret = post.getResponseBodyAsString(); post.releaseConnection(); return ret; } public static void main(String[] args) throws Exception { String ret = Ping.pingBaidu("最代码", "http://www.zuidaima.com/", "http://www.zuidaima.com/share/1787210045197312.htm", "http://www.zuidaima.com/share/rss.htm"); System.out.println(ret); ret = Ping.pingGoogle("最代码", "http://www.zuidaima.com/", "http://www.zuidaima.com/share/1787210045197312.htm", "http://www.zuidaima.com/share/rss.htm"); System.out.println(ret); } }
运行截图如下:
<?xml version="1.0" encoding="UTF-8"?> <methodResponse> <params> <param> <value> <int>0</int> </value> </param> </params> </methodResponse> <?xml version="1.0"?> <methodResponse><params> <param><value><struct> <member> <name>flerror</name><value><boolean>0</boolean></value> </member> <member> <name>message</name><value>Thanks for the ping.</value> </member> </struct></value></param> </params></methodResponse>
另外找了份php的代码,附上来给大家做参考,有需要的下载吧
/** +------------------------------------------------------------------------------ * 通知搜索引擎过来抓去最新发布的内容。秒收不是梦 * 目前仅支持Google和Baidu +------------------------------------------------------------------------------ */ class ping { public $method, $callback; public function method($site_name, $site_url, $update_url, $update_rss) { $this->method = " <?xml version="1.0" encoding="UTF-8"?> <methodCall> <methodName>weblogUpdates.extendedPing</methodName> <params> <param><value>{$site_name}</value></param> <param><value>{$site_url}</value></param> <param><value>{$update_url}</value></param> <param><value>{$update_rss}</value></param> </params> </methodCall>"; return $this->method; } public function _post($url, $postvar) { $ch = curl_init(); $headers = array( "POST " . $url . " HTTP/1.0", "Content-type: text/xml;charset="utf-8"", "Accept: text/xml", "Content-length: " . strlen($postvar) ); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $postvar); $res = curl_exec($ch); curl_close($ch); return $res; } public function google() { $this->callback = $this->_post('http://blogsearch.google.com/ping/RPC2', $this->method); return strpos($this->callback, "<boolean>0</boolean>") ? true : false; } public function baidu() { $this->callback = $this->_post('http://ping.baidu.com/ping/RPC2', $this->method); return strpos($this->callback, "<int>0</int>") ? true : false; } }
?