getComponent() === null) { throw new InvalidConfigException('Required "reCaptcha" component as instance of ' . Component::class . '.'); } $this->when = function() { return !YII_ENV_TEST; }; } /** * @inheritdoc */ protected function validateValue($value) { if (empty($value)) { return [$this->requiredMessage, []]; } $response = $this->createClient()->post(self::SITE_VERIFY_URL, [ 'form_params' => [ 'secret' => $this->getComponent()->secret, 'response' => $value, 'remoteip' => Yii::$app->getRequest()->getUserIP(), ], ]); $data = json_decode($response->getBody(), true); if (!isset($data['success'])) { throw new Exception('Invalid recaptcha verify response.'); } return $data['success'] ? null : [$this->message, []]; } /** * @return Component */ protected function getComponent() { return Yii::$app->reCaptcha; } protected function createClient() { return new GuzzleClient(); } }