We help IT Professionals succeed at work.
Get Started

How to display data contained in JSON object?

dlearman1
dlearman1 asked
on
90 Views
Last Modified: 2020-03-11
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": [...]        
}

Open in new window


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");
        } 
    ?>

Open in new window


Tried echo $responseKeys[success]; and var_dump($responseKeys. They don't appear to work.
Comment
Watch Question
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE