Diagnosing the missing-input-response Error in reCAPTCHA v2
When validating a reCAPTCHA challenge via a backend endpoint, encountering the error code missing-input-response usually points to a specific data integrity issue rather than a network failure or an invalid secret key. Based on the PHP snippet you provided and the resulting JSON payload containing [error-codes] => [0] => missing-input-response, the core problem is that the request sent to Google's verification API lacks the response parameter entirely, or it contains a value that fails strict validation before the server processes it.
In your code, $response = $_POST['g-recaptcha-respone']; attempts to capture the token. The first red flag is immediately visible in the variable assignment: you have a typo in the field name. The standard HTML input field for reCAPTCHA v2 is named g-recaptcha-response. Your code references g-recaptcha-respone (missing the final 's'). While PHP's loose typing might sometimes handle this if the form data contains both keys, relying on a misspelled key is an anti-pattern that leads to undefined variables or empty strings. If $_POST['g-recaptcha-respone'] resolves to an empty string "", some API implementations treat this differently than a missing parameter, but often it results in the exact error you are seeing because the payload is effectively null to the verification service.
Beyond the typo, there is a critical security and reliability concern in how you are fetching the result: using file_get_contents() directly on an unvalidated URL. This approach bypasses Laravel's built-in HTTP client protections unless you have explicitly configured them globally. More importantly, constructing the URL via string interpolation ("https://...&secret=$secret...") is fragile and prone to injection risks if the input isn't strictly sanitized before being used in the query string. While the error message specifically complains about the missing response, fixing the typo is only half the solution; you must ensure that the value retrieved from $_POST is not empty before sending it to Google.
Here is a corrected implementation pattern using the native PHP cURL extension, which offers better control over timeouts and error reporting than file_get_contents:
$secret = 'YOUR_SECRET_KEY';
$response = $_POST['g-recaptcha-response'] ?? null; // Corrected field name with null coalescing
$remoteIp = $_SERVER['REMOTE_ADDR'];
// Only proceed if a response token is actually present
if ($response === null || $response === '') {
return response()->json(['error' => 'reCAPTCHA response missing'], 400);
}
$ch = curl_init();
$url = "https://www.google.com/recaptcha/api/siteverify";
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query([
'secret' => $secret,
'response' => $response,
'remoteip' => $remoteIp,
]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10, // Prevent hanging if Google's API is unreachable
]);
$result_json = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
return response()->json(['error' => 'Network error contacting reCAPTCHA'], 503);
}
$resulting = json_decode($result_json, true);
if ($resulting['success']) {
// Proceed with login or form submission
} else {
if (isset($resulting['error-codes'][0])) {
// Log the specific error code for debugging
log_error("reCAPTCHA failed: " . $resulting['error-codes'][0]);
}
}
The logic here introduces a vital check: if ($response === null || $response === ''). If your form does not submit a token (perhaps due to JavaScript errors or the user clicking "I'm not a robot" without waiting for the widget to render), sending an empty string to Google triggers validation failures. By intercepting this state on your backend, you can return a clear HTTP 400 Bad Request status rather than relying solely on Google's specific error codes.
Additionally, consider migrating from the deprecated v2 checkbox widget to reCAPTCHA v3 if you are still using the older implementation. v3 operates invisibly and returns a score between 0.0 and 1.0, eliminating the need for a visible checkbox and reducing friction for users while maintaining robust spam protection. If you are building this within a Laravel application, leveraging the framework's HTTP client or dedicated packages can streamline this process further, ensuring that API calls are handled consistently across your entire stack.