socket = $socket; } else { $this->socket = new Socket(); } } /** * 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) { $errno = 0; $errstr = ''; if ($this->socket->fsockopen('ssl://' . self::RECAPTCHA_HOST, 443, $errno, $errstr, 30) !== false) { $content = $params->toQueryString(); $request = "POST " . self::SITE_VERIFY_PATH . " HTTP/1.1\r\n"; $request .= "Host: " . self::RECAPTCHA_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')) { $parts = preg_split("#\n\s*\n#Uis", $response); return $parts[1]; } return self::BAD_RESPONSE; } return self::BAD_REQUEST; } }