Commit 33c327773cbe95553424cf4e72cac4a1c6e74024
1 parent
03209e58
ap testing
Showing
1 changed file
with
336 additions
and
2 deletions
Show diff stats
app/library/App/Controllers/AllPositionController.php
... | ... | @@ -21,7 +21,7 @@ class AllPositionController extends CrudResourceController |
21 | 21 | |
22 | 22 | try |
23 | 23 | { |
24 | - $APObj = new \xf3\AllPositions('150bb253651622507341cd7845f5b9d2'); | |
24 | + $APObj = new AllPositions('150bb253651622507341cd7845f5b9d2'); | |
25 | 25 | |
26 | 26 | $project_info = $APObj->get_project(418068); |
27 | 27 | } |
... | ... | @@ -38,4 +38,338 @@ class AllPositionController extends CrudResourceController |
38 | 38 | |
39 | 39 | } |
40 | 40 | |
41 | -} | |
42 | 41 | \ No newline at end of file |
42 | +} | |
43 | + | |
44 | + | |
45 | +/** | |
46 | + * Class AllPositions | |
47 | + * | |
48 | + * Provides simple access to allpositions.ru API | |
49 | + * | |
50 | + * For full methods reference, see http://allpositions.ru/help/api/ | |
51 | + */ | |
52 | +class AllPositions { | |
53 | + public $apiKey = ''; | |
54 | + | |
55 | + /** | |
56 | + * @var string Last occured error message | |
57 | + */ | |
58 | + private $_lastError = null; | |
59 | + | |
60 | + /** | |
61 | + * @var \xmlrpc_client | |
62 | + */ | |
63 | + private $_client = null; | |
64 | + | |
65 | + /** | |
66 | + * Creates new allpositions.ru API client | |
67 | + * | |
68 | + * @param string $apiKey allpositions.ru API key | |
69 | + */ | |
70 | + public function __construct($apiKey = '') { | |
71 | + $this->apiKey = $apiKey; | |
72 | + } | |
73 | + | |
74 | + /** | |
75 | + * Creates API XMLRPC client | |
76 | + * | |
77 | + * @return \xmlrpc_client | |
78 | + */ | |
79 | + private function _getClient() { | |
80 | + if ($this->_client === null) { | |
81 | + if (!$this->apiKey) { | |
82 | + $this->_lastError = 'No API key provided'; | |
83 | + return false; | |
84 | + } | |
85 | + | |
86 | + $this->_client = new \xmlrpc_client('api', 'allpositions.ru', 80); | |
87 | + | |
88 | + $GLOBALS ['xmlrpc_defencoding'] = "UTF8"; | |
89 | + $GLOBALS ['xmlrpc_internalencoding'] = "UTF-8"; | |
90 | + $this->_client->request_charset_encoding = 'UTF-8'; | |
91 | + | |
92 | + $this->_client->setcookie('api_key', $this->apiKey, '/', 'allpositions.ru'); | |
93 | + } | |
94 | + | |
95 | + return $this->_client; | |
96 | + } | |
97 | + | |
98 | + /** | |
99 | + * Calls specified API method with optional arguments | |
100 | + * | |
101 | + * @param string $method API method name | |
102 | + * @param array $arguments Method arguments. Each item is an array in format:<pre> | |
103 | + * [0] => Argument value | |
104 | + * [1] => Argument type ('array', 'int', 'string') | |
105 | + * [2] => If not empty - marks argument as optional. In this case, if value is null, argument won't be passed | |
106 | + * </pre> | |
107 | + * | |
108 | + * @return mixed null on error | |
109 | + */ | |
110 | + private function _request($method, $arguments = array()) { | |
111 | + $client = $this->_getClient(); | |
112 | + | |
113 | + if (!$client) { | |
114 | + return null; | |
115 | + } | |
116 | + | |
117 | + $params = array(); | |
118 | + | |
119 | + foreach($arguments as $argument) { | |
120 | + if (!$argument || !empty($argument[2]) && $argument[0] === null) break; | |
121 | + | |
122 | + $params[]= new \xmlrpcval($argument[0], $argument[1]); | |
123 | + } | |
124 | + | |
125 | + $msg = new \xmlrpcmsg($method, $params); | |
126 | + | |
127 | + $res = $client->send($msg); | |
128 | + | |
129 | + if ($res->faultCode()) { | |
130 | + $this->_lastError = $res->faultString(); | |
131 | + return null; | |
132 | + } | |
133 | + | |
134 | + $this->_lastError = null; | |
135 | + | |
136 | + return php_xmlrpc_decode($res->value()); | |
137 | + } | |
138 | + | |
139 | + /** | |
140 | + * @see http://allpositions.ru/help/api/#add_queries | |
141 | + * | |
142 | + * @param int $projectID Project ID | |
143 | + * @param string $queries Queries divided by \n | |
144 | + * @param int $groupID [Optional] Group ID | |
145 | + * | |
146 | + * @return bool | |
147 | + */ | |
148 | + public function add_queries($projectID, $queries, $groupID = null) { | |
149 | + return $this->_request( | |
150 | + 'add_queries', | |
151 | + array( | |
152 | + array( | |
153 | + $projectID, 'int' | |
154 | + ), | |
155 | + array( | |
156 | + $queries, 'string' | |
157 | + ), | |
158 | + array( | |
159 | + $groupID, 'int', true | |
160 | + ), | |
161 | + ) | |
162 | + ); | |
163 | + } | |
164 | + | |
165 | + /** | |
166 | + * @see http://allpositions.ru/help/api/#delete_queries | |
167 | + * | |
168 | + * @param array $ids Queries IDs | |
169 | + * | |
170 | + * @return bool | |
171 | + */ | |
172 | + public function delete_queries($ids) { | |
173 | + return $this->_request( | |
174 | + 'delete_queries', | |
175 | + array( | |
176 | + array( | |
177 | + $ids, 'array' | |
178 | + ), | |
179 | + ) | |
180 | + ); | |
181 | + } | |
182 | + | |
183 | + /** | |
184 | + * @see http://allpositions.ru/help/api/#get_project | |
185 | + * | |
186 | + * @param int $projectID | |
187 | + * | |
188 | + * @return array | |
189 | + */ | |
190 | + public function get_project($projectID) { | |
191 | + return $this->_request( | |
192 | + 'get_project', | |
193 | + array( | |
194 | + array( | |
195 | + $projectID, 'int', | |
196 | + ), | |
197 | + ) | |
198 | + ); | |
199 | + } | |
200 | + | |
201 | + /** | |
202 | + * @see http://allpositions.ru/help/api/#get_projects | |
203 | + * | |
204 | + * @param int $groupID [Optional] group ID | |
205 | + * | |
206 | + * @return array | |
207 | + */ | |
208 | + public function get_projects($groupID = null) { | |
209 | + return $this->_request( | |
210 | + 'get_projects', | |
211 | + array( | |
212 | + array( | |
213 | + $groupID, 'int', true | |
214 | + ), | |
215 | + ) | |
216 | + ); | |
217 | + } | |
218 | + | |
219 | + /** | |
220 | + * @see http://allpositions.ru/help/api/#get_projects_group | |
221 | + * | |
222 | + * @return array | |
223 | + */ | |
224 | + public function get_projects_group() { | |
225 | + return $this->_request('get_projects_group'); | |
226 | + } | |
227 | + | |
228 | + /** | |
229 | + * @see http://allpositions.ru/help/api/#get_queries | |
230 | + * | |
231 | + * @param int $projectID Project ID | |
232 | + * @param int $groupID [Optional] Group ID | |
233 | + * | |
234 | + * @return array | |
235 | + */ | |
236 | + public function get_queries($projectID, $groupID = null) { | |
237 | + return $this->_request( | |
238 | + 'get_queries', | |
239 | + array( | |
240 | + array( | |
241 | + $projectID, 'int' | |
242 | + ), | |
243 | + array( | |
244 | + $groupID, 'int', true | |
245 | + ), | |
246 | + ) | |
247 | + ); | |
248 | + } | |
249 | + | |
250 | + /** | |
251 | + * @see http://allpositions.ru/help/api/#get_queries_group | |
252 | + * | |
253 | + * @param int $projectID Project ID | |
254 | + * | |
255 | + * @return array | |
256 | + */ | |
257 | + public function get_queries_group($projectID) { | |
258 | + return $this->_request( | |
259 | + 'get_queries_group', | |
260 | + array( | |
261 | + array( | |
262 | + $projectID, 'int' | |
263 | + ), | |
264 | + ) | |
265 | + ); | |
266 | + } | |
267 | + | |
268 | + /** | |
269 | + * @see http://allpositions.ru/help/api/#get_report | |
270 | + * | |
271 | + * @param int $projectID Project ID | |
272 | + * @param string $date [Optional] Report date (Y-m-d, e.g. '2014-05-20') | |
273 | + * @param string $prevDate [Optional] Date to compare results from (Y-m-d, e.g. '2014-05-20') | |
274 | + * @param int $page [Optional] Page number | |
275 | + * @param int $perPage [Optional] Rows on page | |
276 | + * | |
277 | + * @return array | |
278 | + */ | |
279 | + public function get_report($projectID, $date = null, $prevDate = null, $page = null, $perPage = null) { | |
280 | + return $this->_request( | |
281 | + 'get_report', | |
282 | + array( | |
283 | + array( | |
284 | + $projectID, 'int' | |
285 | + ), | |
286 | + array( | |
287 | + $date, 'string', true | |
288 | + ), | |
289 | + array( | |
290 | + $prevDate, 'string', true | |
291 | + ), | |
292 | + array( | |
293 | + $page, 'int', true | |
294 | + ), | |
295 | + array( | |
296 | + $perPage, 'int', true | |
297 | + ), | |
298 | + ) | |
299 | + ); | |
300 | + } | |
301 | + | |
302 | + /** | |
303 | + * @see http://allpositions.ru/help/api/#get_report_dates | |
304 | + * | |
305 | + * @param int $projectID Project ID | |
306 | + * | |
307 | + * @return array | |
308 | + */ | |
309 | + public function get_report_dates($projectID) { | |
310 | + return $this->_request( | |
311 | + 'get_report_dates', | |
312 | + array( | |
313 | + array( | |
314 | + $projectID, 'int' | |
315 | + ), | |
316 | + ) | |
317 | + ); | |
318 | + } | |
319 | + | |
320 | + /** | |
321 | + * @see http://allpositions.ru/help/api/#get_ses | |
322 | + * | |
323 | + * @param int $projectID Project ID | |
324 | + * | |
325 | + * @return array | |
326 | + */ | |
327 | + public function get_ses($projectID) { | |
328 | + return $this->_request( | |
329 | + 'get_ses', | |
330 | + array( | |
331 | + array( | |
332 | + $projectID, 'int' | |
333 | + ) | |
334 | + ) | |
335 | + ); | |
336 | + } | |
337 | + | |
338 | + /** | |
339 | + * @see http://allpositions.ru/help/api/#get_visibility | |
340 | + * | |
341 | + * @param int $projectID Project ID | |
342 | + * @param string $beginDate [Optional] Begin date (Y-m-d, e.g. '2014-05-20') | |
343 | + * @param string $endDate [Optional] End date (Y-m-d, e.g. '2014-05-20') | |
344 | + * @param int $seID [Optional] Search engine ID | |
345 | + * | |
346 | + * @return array | |
347 | + */ | |
348 | + public function get_visibility($projectID, $beginDate = null, $endDate = null, $seID = null) { | |
349 | + return $this->_request( | |
350 | + 'get_visibility', | |
351 | + array( | |
352 | + array( | |
353 | + $projectID, 'int' | |
354 | + ), | |
355 | + array( | |
356 | + $beginDate, 'string', true | |
357 | + ), | |
358 | + array( | |
359 | + $endDate, 'string', true | |
360 | + ), | |
361 | + array( | |
362 | + $seID, 'int', true | |
363 | + ), | |
364 | + ) | |
365 | + ); | |
366 | + } | |
367 | + | |
368 | + /** | |
369 | + * Returns last occured error message | |
370 | + * | |
371 | + * @return string | |
372 | + */ | |
373 | + public function lastError() { | |
374 | + return $this->_lastError; | |
375 | + } | |
376 | +} | ... | ... |