Skip to content

Instantly share code, notes, and snippets.

@mcaskill
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mcaskill/02029dcfe8bb660fcbb0 to your computer and use it in GitHub Desktop.
Save mcaskill/02029dcfe8bb660fcbb0 to your computer and use it in GitHub Desktop.
ReCAPTCHA 2 Error Code Parsing
<?php
/**
* Parse ReCAPTCHA API verion 2.0 error codes.
*
* @uses mcaskill\ReCAPTCHA or jmcastagnetto\ReCAPTCHA
* @link https://developers.google.com/recaptcha/docs/verify Error code reference
*
* @param mixed $response A Google\ReCaptcha\Response or null
* @param array $arr If the second parameter $arr is present, error messages are stored in this variable as associative array elements instead.
* @return array $arr
*/
function parse_recaptcha_response_errors( $response = null, & $arr = array() )
{
$_error_message = 'An unknown error or malformed response has occured.');
$_error_code = 'unknown-error-response';
if ( ! isset( $response->errorCodes ) ) {
$arr[ $_error_code ] = $_error_message;
return $arr;
}
$codes = $response->errorCodes;
if ( ! is_array( $codes ) ) {
$codes = [ $codes ];
}
$codes = array_filter( $codes, 'strlen' );
if ( ! count( $codes ) ) {
$arr[ $_error_code ] = $_error_message;
return $arr;
}
foreach ( $codes as $code ) {
switch ( $code ) {
case 'missing-input-secret':
$arr[ $code ] = 'The secret parameter is missing.';
break;
case 'invalid-input-secret':
$arr[ $code ] = 'The secret parameter is invalid or malformed.';
break;
case 'missing-input':
case 'missing-input-response':
$arr[ $code ] = 'The response parameter is missing.';
break;
case 'invalid-input':
case 'invalid-input-response':
$arr[ $code ] = 'The response parameter is invalid or malformed.';
break;
default:
$arr[ $code ] = $_error_message;
break;
}
}
return $arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment