You are here

class CurlPost in reCAPTCHA 6.2

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

Sends cURL request to the reCAPTCHA service. Note: this requires the cURL extension to be enabled in PHP

Hierarchy

Expanded class hierarchy of CurlPost

See also

http://php.net/manual/en/book.curl.php

File

recaptcha-php/src/ReCaptcha/RequestMethod/CurlPost.php, line 37

Namespace

ReCaptcha\RequestMethod
View source
class CurlPost implements RequestMethod {

  /**
   * URL to which requests are sent via cURL.
   * @const string
   */
  const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';

  /**
   * Curl connection to the reCAPTCHA service
   * @var Curl
   */
  private $curl;
  public function __construct(Curl $curl = null) {
    if (!is_null($curl)) {
      $this->curl = $curl;
    }
    else {
      $this->curl = new Curl();
    }
  }

  /**
   * Submit the cURL request with the specified parameters.
   *
   * @param RequestParameters $params Request parameters
   * @return string Body of the reCAPTCHA response
   */
  public function submit(RequestParameters $params) {
    $handle = $this->curl
      ->init(self::SITE_VERIFY_URL);
    $options = array(
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => $params
        ->toQueryString(),
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/x-www-form-urlencoded',
      ),
      CURLINFO_HEADER_OUT => false,
      CURLOPT_HEADER => false,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_SSL_VERIFYPEER => true,
    );
    $this->curl
      ->setoptArray($handle, $options);
    $response = $this->curl
      ->exec($handle);
    $this->curl
      ->close($handle);
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurlPost::$curl private property Curl connection to the reCAPTCHA service
CurlPost::SITE_VERIFY_URL constant URL to which requests are sent via cURL. @const string
CurlPost::submit public function Submit the cURL request with the specified parameters. Overrides RequestMethod::submit
CurlPost::__construct public function