Httpcws.class.php
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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;
}
}