php引用discuz地址獲取關鍵詞(www.rijigu.com)

gfans0726發表於2011-12-06

為了獲取文章內容的關鍵詞,找了很多方法,感覺這個還不錯,拿出來分享給大家。如果大家想要看實際效果的話,去www.rijigu.com文章的釋出頁,那裡能看到效果的。希望大家多多支援

function sw_discuz($contents){
		$str = strip_tags($contents);
		$arr = array(' ',' ',"\s", "\r\n", "\n", "\r", "\t", ">", "“", "”","<br />");
		$str = str_replace($arr, '', $str);
		if(strlen($str)>2400){
			$str = substr($str, '0', '2400');
		}
		$str = rawurlencode($str); // 初始化一個 cURL 物件 
		$url = "http://keyword.discuz.com/related_kw.html?title=$str&ics=utf-8&ocs=utf-8";
		$curl = curl_init(); // 將urlencode後的字串提交到discuz的分詞URL
		curl_setopt($curl, CURLOPT_URL,$url); 
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
		$data = curl_exec($curl);
		$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
		$words_tmp = $xml -> keyword ->result;
		$wordsArray = array();
		foreach($words_tmp -> item as $val){
			$kw = (array)$val -> kw;
			$wordsArray[] = $kw[0];
		}
		return $wordsArray;
	}



相關文章