最代码广告位
最代码官方的gravatar头像
最代码官方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;
    }
  
}

打赏

最代码最近下载分享源代码列表最近下载
最代码最近浏览分享源代码列表最近浏览
wdh843 LV32019年12月17日
星星星星星星
allen720 LV92019年12月4日
月亮月亮星星
123hdhdhd LV102019年11月27日
月亮月亮星星星星
xingchenStar LV42019年11月18日
月亮
最代码安逸 LV132019年7月20日
月亮月亮月亮星星
吃爪爪2018年11月8日
暂无贡献等级
aizhuzhuqq LV22018年9月7日
星星星星
kayok LV192018年8月2日
太阳星星星星星星
月之氏族 LV212018年7月18日
太阳月亮星星
vijayChen LV162018年7月4日
太阳
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友