You are here

class ReCaptcha in reCAPTCHA 6.2

Same name and namespace in other branches
  1. 8.2 recaptcha-php/src/ReCaptcha/ReCaptcha.php \ReCaptcha\ReCaptcha
  2. 7.2 recaptcha-php/src/ReCaptcha/ReCaptcha.php \ReCaptcha\ReCaptcha

reCAPTCHA client.

Hierarchy

Expanded class hierarchy of ReCaptcha

File

recaptcha-php/src/ReCaptcha/ReCaptcha.php, line 32

Namespace

ReCaptcha
View source
class ReCaptcha {

  /**
   * Version of this client library.
   * @const string
   */
  const VERSION = 'php_1.1.2';

  /**
   * Shared secret for the site.
   * @var type string
   */
  private $secret;

  /**
   * Method used to communicate  with service. Defaults to POST request.
   * @var RequestMethod
   */
  private $requestMethod;

  /**
   * Create a configured instance to use the reCAPTCHA service.
   *
   * @param string $secret shared secret between site and reCAPTCHA server.
   * @param RequestMethod $requestMethod method used to send the request. Defaults to POST.
   */
  public function __construct($secret, RequestMethod $requestMethod = null) {
    if (empty($secret)) {
      throw new \RuntimeException('No secret provided');
    }
    if (!is_string($secret)) {
      throw new \RuntimeException('The provided secret must be a string');
    }
    $this->secret = $secret;
    if (!is_null($requestMethod)) {
      $this->requestMethod = $requestMethod;
    }
    else {
      $this->requestMethod = new RequestMethod\Post();
    }
  }

  /**
   * Calls the reCAPTCHA siteverify API to verify whether the user passes
   * CAPTCHA test.
   *
   * @param string $response The value of 'g-recaptcha-response' in the submitted form.
   * @param string $remoteIp The end user's IP address.
   * @return Response Response from the service.
   */
  public function verify($response, $remoteIp = null) {

    // Discard empty solution submissions
    if (empty($response)) {
      $recaptchaResponse = new Response(false, array(
        'missing-input-response',
      ));
      return $recaptchaResponse;
    }
    $params = new RequestParameters($this->secret, $response, $remoteIp, self::VERSION);
    $rawResponse = $this->requestMethod
      ->submit($params);
    return Response::fromJson($rawResponse);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReCaptcha::$requestMethod private property Method used to communicate with service. Defaults to POST request.
ReCaptcha::$secret private property Shared secret for the site.
ReCaptcha::verify public function Calls the reCAPTCHA siteverify API to verify whether the user passes CAPTCHA test.
ReCaptcha::VERSION constant Version of this client library. @const string
ReCaptcha::__construct public function Create a configured instance to use the reCAPTCHA service.