You are here

class Drupal8Post in reCAPTCHA 8.2

Sends POST requests to the reCAPTCHA service with Drupal 8 httpClient.

Hierarchy

Expanded class hierarchy of Drupal8Post

1 file declares its use of Drupal8Post
recaptcha.module in ./recaptcha.module
Verifies if user is a human without necessity to solve a CAPTCHA.

File

src/ReCaptcha/RequestMethod/Drupal8Post.php, line 12

Namespace

ReCaptcha\RequestMethod
View source
class Drupal8Post implements RequestMethod {

  /**
   * Submit the POST request with the specified parameters.
   *
   * @param \ReCaptcha\ReCaptcha\RequestParameters $params
   *   Request parameters.
   *
   * @return string
   *   Body of the reCAPTCHA response.
   */
  public function submit(RequestParameters $params) {
    $options = [
      'headers' => [
        'Content-type' => 'application/x-www-form-urlencoded',
      ],
      'body' => $params
        ->toQueryString(),
      // Stop firing exception on response status code >= 300.
      // See http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html
      'http_errors' => FALSE,
    ];
    $response = \Drupal::httpClient()
      ->post(ReCaptcha::SITE_VERIFY_URL, $options);
    if ($response
      ->getStatusCode() == 200) {

      // The service request was successful.
      return (string) $response
        ->getBody();
    }
    elseif ($response
      ->getStatusCode() < 0) {

      // Negative status codes typically point to network or socket issues.
      return '{"success": false, "error-codes": ["' . ReCaptcha::E_CONNECTION_FAILED . '"]}';
    }
    else {

      // Positive none 200 status code typically means the request has failed.
      return '{"success": false, "error-codes": ["' . ReCaptcha::E_BAD_RESPONSE . '"]}';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Drupal8Post::submit public function Submit the POST request with the specified parameters. Overrides RequestMethod::submit