You are here

class Post in reCAPTCHA 8.2

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

Sends POST requests to the reCAPTCHA service.

Hierarchy

Expanded class hierarchy of Post

File

recaptcha-php/src/ReCaptcha/RequestMethod/Post.php, line 36

Namespace

ReCaptcha\RequestMethod
View source
class Post implements RequestMethod {

  /**
   * URL for reCAPTCHA sitevrerify API
   * @var string
   */
  private $siteVerifyUrl;

  /**
   * Only needed if you want to override the defaults
   *
   * @param string $siteVerifyUrl URL for reCAPTCHA sitevrerify API
   */
  public function __construct($siteVerifyUrl = null) {
    $this->siteVerifyUrl = is_null($siteVerifyUrl) ? ReCaptcha::SITE_VERIFY_URL : $siteVerifyUrl;
  }

  /**
   * Submit the POST request with the specified parameters.
   *
   * @param RequestParameters $params Request parameters
   * @return string Body of the reCAPTCHA response
   */
  public function submit(RequestParameters $params) {
    $options = array(
      'http' => array(
        'header' => "Content-type: application/x-www-form-urlencoded\r\n",
        'method' => 'POST',
        'content' => $params
          ->toQueryString(),
        // Force the peer to validate (not needed in 5.6.0+, but still works)
        'verify_peer' => true,
      ),
    );
    $context = stream_context_create($options);
    $response = file_get_contents($this->siteVerifyUrl, false, $context);
    if ($response !== false) {
      return $response;
    }
    return '{"success": false, "error-codes": ["' . ReCaptcha::E_CONNECTION_FAILED . '"]}';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Post::$siteVerifyUrl private property URL for reCAPTCHA sitevrerify API
Post::submit public function Submit the POST request with the specified parameters. Overrides RequestMethod::submit
Post::__construct public function Only needed if you want to override the defaults