Httpcws.class.php 1.1 KB
<?php
/**
 * 分词(HttpCWS)
 */
class Util_Ws_Adapter_Httpcws extends Util_Ws_Adapter_Abstract 
{
    /**
     * 服务器地址
     * 
     * @var array
     */
	private $_server;
	
	public function __construct($options = array()) 
	{
		$this->_server = Util_Server::factory('ws', 'httpcws', $options)->loadBalanceServer();
	}
	
	/**
	 * 获取分词
	 * 
	 * @param string $inputs (输入内容)
	 * @return mixed (false:不存在 | array:结果集)
	 */
	public function getWords($inputs) 
	{
	    $result = false;
		if (strlen($inputs) <= 20480) 
		{
		    $words = addslashes($inputs);
			$inputs = @iconv('UTF-8', 'GB2312', $words);
			
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $this->_server['host'] . ':' . $this->_server['port']);
			curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($inputs));
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$result = curl_exec($ch);
			curl_close($ch);
			
			if ($result !== false)
			{
			    $result = iconv('GB2312', 'UTF-8', $result);
			    $result = array_filter(explode(' ', $result));
			    array_push($result, $words);
			}
		}
		return $result;
	}
	
}