用php實現Google /Baidu Ping服務快速收錄

玩過WORDPRESS的朋友應該都知道ping服務吧,通俗點講它可以在更新文章的時候向Google、baidu及其他支持ping的搜索引擎發送指令然後招呼它們過來,不用傻等他們過來收錄了,化被動爲主動了。直接貼代碼了,很簡單的。


/**
  +------------------------------------------------------------------------------
 * 通知搜索引擎過來抓去最新發布的內容。秒收不是夢
 * 目前僅支持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;
    }
  
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章