troubleshooting Question

How to display data contained in JSON object?

Avatar of dlearman1
dlearman1Flag for United States of America asked on
GooglePHPJSON
6 Comments1 Solution95 ViewsLast Modified:
I would like to output (for curiosity/debugging) the data values in the JSON object returned by Google ReCaptcha v3 verification. The ReCaptcha doc's show the object's format to be:
{
  "success": true|false,      
  "score": number,             
  "action": string,           
  "challenge_ts": timestamp,
  "hostname": string,        
  "error-codes": [...]        
}

Here is code snippet:
<?php
.
.
.
 // post request to verify token with Google reCaptcha server
      //
      $url = "https://www.google.com/recaptcha/api/siteverify";
      $data = array (
        'secret' => $secretKey, 
        'response' => $captcha,
      );
         
      // use key 'http' even if request is to https://...
      //
      $options = array (
        'http' => array (
          'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
          'method'  => 'POST',
          'content' => http_build_query($data)
        )
      );

      $context  = stream_context_create($options);
      $response = file_get_contents($url, false, $context);

      $responseKeys = json_decode($response, true);

     // echo $responseKeys[success];
     // echo $responseKeys[action];
      //echo $responseKeys[score];
 
      if($responseKeys["success"] == true && $responseKeys["action"] == $action && $responseKeys["score"] >= 0.5) {
        echo ("reCaptcha.php thinks you are human");
        header ("location: ./sent-success.htm");
        } else {
          echo ("reCaptcha.php thinks you are a bot");
          header ("location: ./spam-block.htm");
        } 
    ?>

Tried echo $responseKeys[success]; and var_dump($responseKeys. They don't appear to work.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros