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