最代码官方
2014-05-14 23:41:43
原证精
seo优化之Google和Baidu Ping服务实现快速收录文章的java,php代码实现
最近在做关于google和百度的ping服务,希望能提高搜索引擎蜘蛛的抓取频率,搜索了大半天都不太好用,所以自己写了一份,大家可以参考下:
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; } }
猜你喜欢
请下载代码后再发表评论
相关代码
最近下载
最近浏览
微信网友_6715964359839744 LV1
2023年10月31日
kkkkioio LV7
2023年7月3日
1751216397 LV1
2023年3月22日
81013466 LV1
2023年2月12日
快乐的程序员 LV25
2023年1月22日
微信网友_6040315240812544 LV8
2022年10月27日
qwqw900619 LV4
2022年8月29日
蛇头凌志 LV8
2022年8月20日
远程勘验 LV3
2022年8月8日
crosa_Don LV18
2022年7月20日