reCaptcha; } public function init() { parent::init(); if ($this->getComponent() === null) { throw new InvalidConfigException('Required "reCaptcha" component as instance of ' . Component::class . '.'); } if ($this->message === null) { $this->message = Yii::t('yii', 'The verification code is incorrect.'); } } /** * @inheritdoc */ protected function validateValue($value) { $value = Yii::$app->request->post(self::CAPTCHA_RESPONSE_FIELD); if (empty($value)) { return [$this->message, []]; } $requestParams = [ 'secret' => $this->getComponent()->secret, 'response' => $value, 'remoteip' => Yii::$app->request->userIP, ]; $requestUrl = self::SITE_VERIFY_URL . '?' . http_build_query($requestParams); $response = $this->getResponse($requestUrl); if (!isset($response['success'])) { throw new Exception('Invalid recaptcha verify response.'); } return $response['success'] ? null : [$this->message, []]; } protected function getResponse($request) { $response = file_get_contents($request); return json_decode($response, true); } }