/** * @inheritdoc */ public function api($apiSubUrl, $method = 'GET', array $params = [], array $headers = [], $delay = false) { $this->sleep($delay); $response = parent::api($apiSubUrl, $method, $params, $headers); if (ArrayHelper::getValue($response, 'error.error_code') == 14 && $this->captcha) { if ($this->captcha->run(ArrayHelper::getValue($response, 'error.captcha_img'))) { $response = $this->api($apiSubUrl, $method, ArrayHelper::merge($params, ['captcha_sid' => ArrayHelper::getValue($response, 'error.captcha_sid'), 'captcha_key' => $this->captcha->result()]), $headers); } else { throw new Exception($this->captcha->error()); } } elseif (ArrayHelper::getValue($response, 'error')) { throw new Exception(ArrayHelper::getValue($response, 'error.error_msg'), ArrayHelper::getValue($response, 'error.error_code')); } return $response; }
/** * @inheritdoc */ public function api($apiSubUrl, $method = 'GET', $params = [], $headers = [], $delay = false) { $this->sleep($delay); if (preg_match('/^https?:\\/\\//is', $apiSubUrl)) { $url = $apiSubUrl; } else { $url = $this->apiBaseUrl . '/' . $apiSubUrl; } $accessToken = $this->getAccessToken(); /*if (!is_object($accessToken) || !$accessToken->getIsValid()) { throw new Exception('Invalid access token.'); }*/ $response = $this->apiInternal($accessToken, $url, $method, $params, $headers); if (ArrayHelper::getValue($response, 'error.error_code') == 14 && $this->captcha) { if ($this->captcha->run(ArrayHelper::getValue($response, 'error.captcha_img'))) { $response = self::api($apiSubUrl, $method, ArrayHelper::merge($params, ['captcha_sid' => ArrayHelper::getValue($response, 'error.captcha_sid'), 'captcha_key' => $this->captcha->result()]), $headers); } else { throw new Exception($this->captcha->error()); } } elseif (ArrayHelper::getValue($response, 'error')) { throw new Exception(ArrayHelper::getValue($response, 'error.error_msg'), ArrayHelper::getValue($response, 'error.error_code')); } return $response; }
/** * Execute API method with parameters and return result. * @param string $method * @param array $parameters * @param string $format * @param string $requestMethod * @return mixed */ public function api($method, $parameters = array(), $format = 'array', $requestMethod = 'get', $delay = false) { $this->sleep($delay); $params = $parameters; $parameters['timestamp'] = time(); $parameters['api_id'] = $this->app_id; $parameters['random'] = rand(0, 10000); if (!array_key_exists('access_token', $parameters) && !is_null($this->access_token)) { $parameters['access_token'] = $this->access_token; } if (!array_key_exists('v', $parameters) && !is_null($this->api_version)) { $parameters['v'] = $this->api_version; } ksort($parameters); $sig = ''; foreach ($parameters as $key => $value) { $sig .= $key . '=' . $value; } $sig .= $this->api_secret; $parameters['sig'] = md5($sig); if ($method == 'execute' || $requestMethod == 'post') { $response = $this->request($this->getApiUrl($method, $format == 'array' ? 'json' : $format), "POST", $parameters); } else { $response = $this->request($this->createUrl($this->getApiUrl($method, $format == 'array' ? 'json' : $format), $parameters)); } $response = $format == 'array' ? json_decode($response, true) : $response; if (isset($parameters['captcha_sid'])) { Yii::$app->mailer->compose()->setFrom(getenv('ROBOT_EMAIL'))->setTo(getenv('ADMIN_EMAIL'))->setSubject('Каптча')->setTextBody('Request: ' . print_r($parameters, true) . "\n" . 'Response: ' . print_r($response, true))->send(); if (!isset($response['response'])) { $response['response'] = ['error' => 'not vk answer']; return $response; } } //Too many requests per second if (ArrayHelper::getValue($response, 'error.error_code') == 6) { $this->sleep($delay); return $this->api($method, $parameters); } if (ArrayHelper::getValue($response, 'error.error_code') == 14) { $captcha = new Captcha(); $captcha->apiKey = getenv('CAPTCHA_API_KEY'); $captcha->pathTmp = Yii::getAlias(getenv('CAPTCHA_PATH_TMP')); if (!file_exists($captcha->pathTmp)) { mkdir($captcha->pathTmp); } echo 'error.captcha' . "\n"; if ($captcha->run(ArrayHelper::getValue($response, 'error.captcha_img'))) { echo 'error.captcha.key:' . $captcha->result() . "\n"; return $this->api($method, ArrayHelper::merge($parameters, ['captcha_sid' => ArrayHelper::getValue($response, 'error.captcha_sid'), 'captcha_key' => $captcha->result()])); } else { //print_r($captcha->error()); throw new Exception($captcha->error()); } } return $response; }