diff --git a/src/ReCaptcha/RequestMethod/CurlPost.php b/src/ReCaptcha/RequestMethod/CurlPost.php index 5ff831a..f38561c 100644 --- a/src/ReCaptcha/RequestMethod/CurlPost.php +++ b/src/ReCaptcha/RequestMethod/CurlPost.php @@ -42,6 +42,7 @@ use ReCaptcha\ReCaptcha; use ReCaptcha\RequestMethod; use ReCaptcha\RequestParameters; +use ReCaptcha\Response; /** * Sends cURL request to the reCAPTCHA service. @@ -98,7 +99,7 @@ public function submit(RequestParameters $params): string return $response; } - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } finally { curl_close($handle); } diff --git a/src/ReCaptcha/RequestMethod/Post.php b/src/ReCaptcha/RequestMethod/Post.php index 77ff691..46d0c97 100644 --- a/src/ReCaptcha/RequestMethod/Post.php +++ b/src/ReCaptcha/RequestMethod/Post.php @@ -42,6 +42,7 @@ use ReCaptcha\ReCaptcha; use ReCaptcha\RequestMethod; use ReCaptcha\RequestParameters; +use ReCaptcha\Response; /** * Sends POST requests to the reCAPTCHA service. @@ -87,6 +88,6 @@ public function submit(RequestParameters $params): string return $response; } - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } } diff --git a/src/ReCaptcha/RequestMethod/SocketPost.php b/src/ReCaptcha/RequestMethod/SocketPost.php index 955a49d..32e9e6e 100644 --- a/src/ReCaptcha/RequestMethod/SocketPost.php +++ b/src/ReCaptcha/RequestMethod/SocketPost.php @@ -42,6 +42,7 @@ use ReCaptcha\ReCaptcha; use ReCaptcha\RequestMethod; use ReCaptcha\RequestParameters; +use ReCaptcha\Response; /** * Sends a POST request to the reCAPTCHA service, but makes use of fsockopen() @@ -76,17 +77,17 @@ public function submit(RequestParameters $params): string $urlParsed = parse_url($this->siteVerifyUrl); if (false === $urlParsed || !isset($urlParsed['host']) || !isset($urlParsed['path'])) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } $handle = fsockopen('ssl://'.$urlParsed['host'], 443, $errno, $errstr, 30); if (false === $handle || 0 !== $errno || '' !== $errstr) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } if (false === stream_set_timeout($handle, 60)) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}'; + return Response::errorJson(ReCaptcha::E_CONNECTION_FAILED); } $content = $params->toQueryString(); @@ -108,13 +109,13 @@ public function submit(RequestParameters $params): string } if (1 !== preg_match('#^HTTP/1\.[01] 200 OK#', $response)) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}'; + return Response::errorJson(ReCaptcha::E_BAD_RESPONSE); } $parts = preg_split("#\n\\s*\n#Uis", $response); if (!is_array($parts) || !isset($parts[1])) { - return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}'; + return Response::errorJson(ReCaptcha::E_BAD_RESPONSE); } return $parts[1]; diff --git a/src/ReCaptcha/Response.php b/src/ReCaptcha/Response.php index 852439b..da37c4b 100644 --- a/src/ReCaptcha/Response.php +++ b/src/ReCaptcha/Response.php @@ -97,6 +97,18 @@ public function __construct(bool $success, array $errorCodes = [], string $hostn $this->errorCodes = $errorCodes; } + /** + * Build an error response JSON string with the specified error code. + * + * @param string $errorCode The error code to return + * + * @return string The JSON string + */ + public static function errorJson(string $errorCode): string + { + return json_encode(['success' => false, 'error-codes' => [$errorCode]]); + } + /** * Build the response from the expected JSON returned by the service. */