You are here

public static function Response::fromJson in reCAPTCHA 8.2

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

Build the response from the expected JSON returned by the service.

Parameters

string $json:

Return value

\ReCaptcha\Response

1 call to Response::fromJson()
ReCaptcha::verify in recaptcha-php/src/ReCaptcha/ReCaptcha.php
Calls the reCAPTCHA siteverify API to verify whether the user passes CAPTCHA test and additionally runs any specified additional checks

File

recaptcha-php/src/ReCaptcha/Response.php, line 82

Class

Response
The response returned from the service.

Namespace

ReCaptcha

Code

public static function fromJson($json) {
  $responseData = json_decode($json, true);
  if (!$responseData) {
    return new Response(false, array(
      ReCaptcha::E_INVALID_JSON,
    ));
  }
  $hostname = isset($responseData['hostname']) ? $responseData['hostname'] : null;
  $challengeTs = isset($responseData['challenge_ts']) ? $responseData['challenge_ts'] : null;
  $apkPackageName = isset($responseData['apk_package_name']) ? $responseData['apk_package_name'] : null;
  $score = isset($responseData['score']) ? floatval($responseData['score']) : null;
  $action = isset($responseData['action']) ? $responseData['action'] : null;
  if (isset($responseData['success']) && $responseData['success'] == true) {
    return new Response(true, array(), $hostname, $challengeTs, $apkPackageName, $score, $action);
  }
  if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
    return new Response(false, $responseData['error-codes'], $hostname, $challengeTs, $apkPackageName, $score, $action);
  }
  return new Response(false, array(
    ReCaptcha::E_UNKNOWN_ERROR,
  ), $hostname, $challengeTs, $apkPackageName, $score, $action);
}