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