Avatar of dlearman1
dlearman1
Flag for United States of America asked on

How to display data contained in JSON object?

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.
GooglePHPJSON

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

Why not just do

print_r($responseKeys);

Open in new window

Julian Hansen

But wait ...
What are you doing on line 34 and 37?

What happens when those lines execute?

Any output you have sent will be wiped as the page will be reloaded.

If you want to see the output either you have to kill the script immediately after output
OR
Disable the redirects
Or (my preference as you don't mess with the flow of the process)
file_put_contents('debug.log',print_r($responseKeys, true));

Open in new window

And then check the debug.log file in the same folder as the primary script.
dlearman1

ASKER
I've tried
$result = print_r($responseKeys);
      echo $result.'\r\n<br>';

Open in new window

Which resulted in console.log displaying 1\r\n<br>. Is this telling me that success and action variables are empty, and score is equal to 1? Or is the code malformed? Obviously not what I want.

I tried
file_put_contents('debug.log',print_r($responseKeys, true));

Open in new window

and looked at FF debugger window, which was empty?  When you say "in the same folder as the primary script," I'm assuming I need to be in the recaptcha.php directory?  Tried the  'command+P' FIND FILE but only showed js files. Probably, I need to read up on using the debugger.

I have eliminated the header ("location: ./sent-success.htm"); statement.  It appears I can trust the reCaptcha js library to restore the original submit button functionality.

Thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dlearman1

ASKER
If I'm following you, this would be the root folder for themagnolia.com. So far, I don't see a debg.log file there.  The site is on a shared remote server and I don't have direct access to any files uphill of the site root folder.

It looks like your preferred method "file_put_contents('debug.log', print_r($responseKeys, true));" isn't working because of PHP server settings. I'm getting error messages
<b>Warning</b>:  file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>D:\inetpub\themagnolia\reCaptcha.php</b> on line <b>58</b><br />
<br />
<b>Warning</b>:  file_get_contents(https://www.google.com/recaptcha/api/siteverify): failed to open stream: no suitable wrapper could be found in <b>D:\inetpub\themagnolia\reCaptcha.php</b> on line <b>58</b><br />

Open in new window


Line 58 is: $response = file_get_contents($url, false, $context);

file_get_contents() is a standard PHP function, so the error must be with the wrapper, But the PHP code verifies with no syntax errors.

Hopefully, I will be able to sort this out with the ISP tomorrow.
Julian Hansen

It looks like your preferred method "file_put_contents('debug.log', print_r($responseKeys, true));" isn't working because of PHP server settings. I'm getting error messages
Not exactly, this error
b>Warning</b>:  file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in <b>D:\inetpub\themagnolia\reCaptcha.php</b> on line <b>58</b><br />
Has nothing to do with my debug statement. The debug uses file_PUT_contents - this is referring to file_GET_contents and is a very common message on shared hosting.

Using file_get_contents to get a remote URL is very often disabled for security reasons. You need to instead use curl() or fsocket() (cUrl is easier)