You are here

public function SocketPost::submit in reCAPTCHA 8.2

Same name and namespace in other branches
  1. 6.2 recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php \ReCaptcha\RequestMethod\SocketPost::submit()
  2. 7.2 recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php \ReCaptcha\RequestMethod\SocketPost::submit()

Submit the POST request with the specified parameters.

Parameters

RequestParameters $params Request parameters:

Return value

string Body of the reCAPTCHA response

Overrides RequestMethod::submit

File

recaptcha-php/src/ReCaptcha/RequestMethod/SocketPost.php, line 64

Class

SocketPost
Sends a POST request to the reCAPTCHA service, but makes use of fsockopen() instead of get_file_contents(). This is to account for people who may be on servers where allow_url_open is disabled.

Namespace

ReCaptcha\RequestMethod

Code

public function submit(RequestParameters $params) {
  $errno = 0;
  $errstr = '';
  $urlParsed = parse_url($this->siteVerifyUrl);
  if (false === $this->socket
    ->fsockopen('ssl://' . $urlParsed['host'], 443, $errno, $errstr, 30)) {
    return '{"success": false, "error-codes": ["' . ReCaptcha::E_CONNECTION_FAILED . '"]}';
  }
  $content = $params
    ->toQueryString();
  $request = "POST " . $urlParsed['path'] . " HTTP/1.1\r\n";
  $request .= "Host: " . $urlParsed['host'] . "\r\n";
  $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
  $request .= "Content-length: " . strlen($content) . "\r\n";
  $request .= "Connection: close\r\n\r\n";
  $request .= $content . "\r\n\r\n";
  $this->socket
    ->fwrite($request);
  $response = '';
  while (!$this->socket
    ->feof()) {
    $response .= $this->socket
      ->fgets(4096);
  }
  $this->socket
    ->fclose();
  if (0 !== strpos($response, 'HTTP/1.1 200 OK')) {
    return '{"success": false, "error-codes": ["' . ReCaptcha::E_BAD_RESPONSE . '"]}';
  }
  $parts = preg_split("#\n\\s*\n#Uis", $response);
  return $parts[1];
}