You are here

class Drupal8Post in hCaptcha 8

Sends POST requests to the hCaptcha service.

Hierarchy

Expanded class hierarchy of Drupal8Post

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

File

src/HCaptcha/Drupal8Post.php, line 13
Custom Drupal 8 request method class for hCaptcha.

Namespace

Drupal\hcaptcha\HCaptcha
View source
class Drupal8Post implements RequestMethod {

  /**
   * Submit the POST request with the specified parameters.
   *
   * @param array $params
   *   Request parameters.
   *
   * @return \stdClass
   *   Body of the hCaptcha response.
   */
  public function submit($url, array $params) {
    $options = array(
      'headers' => array(
        'Content-type' => 'application/x-www-form-urlencoded',
      ),
      'body' => http_build_query($params, '', '&'),
      // 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($url, $options);
    if ($response
      ->getStatusCode() == 200) {

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

      // Negative status codes typically point to network or socket issues.
      $result = '{"success": false, "error-codes": ["connection-failed"]}';
    }
    else {

      // Positive none 200 status code typically means the request has failed.
      $result = '{"success": false, "error-codes": ["bad-response"]}';
    }
    return json_decode($result);
  }

}

Members

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