We help IT Professionals succeed at work.

How to display data contained in JSON object?

dlearman1
dlearman1 asked
on
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
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Why not just do

print_r($responseKeys);

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
$result = print_r($responseKeys);

Open in new window

Not going to work - when you use print_r like this $result will be 1 i.e. the return value of print_r

If you want the contents of print_r you have to supply the second parameter

$result = print_r($responseKeys, true);

Open in new window

Now $result holds the string containing the dump

and looked at FF debugger window, which was empty?
Which it would be because file_put_contents writes the output to a file on your webserver. You need to go and look for the file debug.log in the SAME folder as the primary script called (by primary script I mean if the file_put_contents is in an included or required / require_once file then you have to go back to the script that was actually invoked and look in the folder for that script).

Author

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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)