Skip to content
Drupal7Post.php 1.29 KiB
Newer Older
<?php

namespace ReCaptcha\RequestMethod;

use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;

/**
 * Sends POST requests to the reCAPTCHA service.
 */
class Drupal7Post implements RequestMethod {

  /**
   * Submit the POST request with the specified parameters.
   *
Liam Morland's avatar
Liam Morland committed
   * @param \ReCaptcha\RequestParameters $params
Alexander Hass's avatar
Alexander Hass committed
   *   Request parameters.
   *
   * @return string
   *   Body of the reCAPTCHA response.
   */
  public function submit(RequestParameters $params) {

    $options = array(
      'headers' => array(
        'Content-type' => 'application/x-www-form-urlencoded',
      ),
      'method' => 'POST',
      'data' => $params->toQueryString(),
    );
    $response = drupal_http_request(ReCaptcha::SITE_VERIFY_URL, $options);
    if ($response->code == 200 && isset($response->data)) {
      // The service request was successful.
      return $response->data;
    }
    elseif ($response->code < 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 . '"]}';
    }