1+ <?php
2+ /**
3+ * @link http://www.tintsoft.com/
4+ * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5+ * @license http://www.tintsoft.com/license/
6+ */
7+
8+ namespace aliyun \search ;
9+
10+ use GuzzleHttp \HandlerStack ;
11+ use GuzzleHttp \Client as HttpClient ;
12+ use aliyun \guzzle \subscriber \Rpc ;
13+
14+ /**
15+ * Class Client
16+ * @package aliyun\search
17+ */
18+ class Client
19+ {
20+ /**
21+ * @var string
22+ */
23+ public $ accessKeyId ;
24+
25+ /**
26+ * @var string
27+ */
28+ public $ accessSecret ;
29+
30+ /**
31+ * @var string 应用名称
32+ */
33+ public $ appName ;
34+
35+ /**
36+ * @var string API版本
37+ */
38+ public $ version = '2016-11-01 ' ;
39+
40+ /**
41+ * @var string 网关地址
42+ */
43+ public $ baseUri = 'http://live.aliyuncs.com/ ' ;
44+
45+ /**
46+ * Client constructor.
47+ * @param array $config
48+ * @throws \Exception
49+ */
50+ public function __construct ($ config = [])
51+ {
52+ foreach ($ config as $ name => $ value ) {
53+ $ this ->{$ name } = $ value ;
54+ }
55+ if (empty ($ this ->accessKeyId )) {
56+ throw new \Exception ('The "accessKeyId" property must be set. ' );
57+ }
58+ if (empty ($ this ->accessSecret )) {
59+ throw new \Exception ('The "accessSecret" property must be set. ' );
60+ }
61+
62+ if (empty ($ this ->appName )) {
63+ throw new \Exception ('The "appName" property must be set. ' );
64+ }
65+ }
66+
67+ /**
68+ * 获取Http Client
69+ * @return HttpClient
70+ */
71+ public function getHttpClient ()
72+ {
73+ if (!is_object ($ this ->_httpClient )) {
74+ $ stack = HandlerStack::create ();
75+ $ middleware = new Rpc ([
76+ 'accessKeyId ' => $ this ->accessKeyId ,
77+ 'accessSecret ' => $ this ->accessSecret ,
78+ 'Version ' => $ this ->version
79+ ]);
80+ $ stack ->push ($ middleware );
81+
82+ $ this ->_httpClient = new HttpClient ([
83+ 'base_uri ' => $ this ->baseUri ,
84+ 'handler ' => $ stack ,
85+ 'verify ' => false ,
86+ 'http_errors ' => false ,
87+ 'connect_timeout ' => 3 ,
88+ 'read_timeout ' => 10 ,
89+ 'debug ' => false ,
90+ ]);
91+ }
92+ return $ this ->_httpClient ;
93+ }
94+
95+ /**
96+ * 获取应用列表
97+ * @return \Psr\Http\Message\ResponseInterface
98+ */
99+ public function getApps ()
100+ {
101+ return $ this ->getHttpClient ()->get ('/index ' );
102+ }
103+
104+ /**
105+ * 查看应用信息
106+ * @return \Psr\Http\Message\ResponseInterface
107+ */
108+ public function appStatus ()
109+ {
110+ return $ this ->getHttpClient ()->get ('/ ' . $ this ->appName );
111+ }
112+
113+ /**
114+ * 搜索
115+ * @param array $params
116+ * @return \Psr\Http\Message\ResponseInterface
117+ * @see https://help.aliyun.com/document_detail/29150.html
118+ */
119+ public function search (array $ params )
120+ {
121+ return $ this ->getHttpClient ()->get ('/search ' , ['query ' => $ params ]);
122+ }
123+
124+ /**
125+ * 下拉提示
126+ * @param array $params
127+ * @return \Psr\Http\Message\ResponseInterface
128+ * @see https://help.aliyun.com/document_detail/29151.html
129+ */
130+ public function suggest (array $ params )
131+ {
132+ return $ this ->getHttpClient ()->get ('/suggest ' , ['query ' => $ params ]);
133+ }
134+
135+ /**
136+ * 重建索引
137+ * @param array $params
138+ * @return \Psr\Http\Message\ResponseInterface
139+ * @see https://help.aliyun.com/document_detail/29152.html
140+ */
141+ public function indexRebuild (array $ params )
142+ {
143+ return $ this ->getHttpClient ()->get ('/index/ ' . $ this ->appName , ['query ' => array_merge (['action ' => 'createtask ' ], $ params )]);
144+ }
145+
146+ /**
147+ * 获取错误日志
148+ * @param int $page
149+ * @param int $pageSize
150+ * @param string $sortMode
151+ * @return \Psr\Http\Message\ResponseInterface
152+ */
153+ public function errorLog ($ page , $ pageSize , $ sortMode )
154+ {
155+ return $ this ->getHttpClient ()->get ('/index/error/ ' . $ this ->appName , ['query ' => ['page ' => $ page , 'page_size ' => $ pageSize , 'sort_mode ' => $ sortMode ]]);
156+ }
157+
158+ /**
159+ * 推送数据
160+ * @param string $tableName 要上传数据的表名
161+ * @param array $items 规定JSON格式,如下所示
162+ * @return \Psr\Http\Message\ResponseInterface
163+ */
164+ public function Push ($ tableName , array $ items )
165+ {
166+ return $ this ->getHttpClient ()->post ('/index/doc/ ' . $ this ->appName , [
167+ 'query ' => [
168+ 'action ' => 'push ' ,
169+ 'table_name ' => $ tableName ,
170+ ],
171+ 'form_params ' => [
172+ 'items ' => \GuzzleHttp \json_encode ($ items )
173+ ]
174+ ]);
175+ }
176+ }
0 commit comments