You are here

class Post in reCAPTCHA 6.2

Same name and namespace in other branches
  1. 8.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 35

Namespace

ReCaptcha\RequestMethod
View source
class Post implements RequestMethod {

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

  /**
   * 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) {

    /**
     * PHP 5.6.0 changed the way you specify the peer name for SSL context options.
     * Using "CN_name" will still work, but it will raise deprecated errors.
     */
    $peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
    $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,
        // Force the peer validation to use www.google.com
        $peer_key => 'www.google.com',
      ),
    );
    $context = stream_context_create($options);
    return file_get_contents(self::SITE_VERIFY_URL, false, $context);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Post::SITE_VERIFY_URL constant URL to which requests are POSTed. @const string
Post::submit public function Submit the POST request with the specified parameters. Overrides RequestMethod::submit