skip to Main Content

I have this JSON an I want to get just license_key.

{"data": {"enabled": true,"product_link": "example","license_key": "BS7X4-55UH7-ZG2EV-ME2N5","buyer_email": "[email protected]","uses": 0,"date": "2022-11-11T20:56:37+00:00"}}

I try this script but when i open it I get white page

<?php
$url = "https://example.com/";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$resp = curl_exec($curl);
curl_close($curl);

$json_decoded = json_decode($resp);

$license_key = '';

for ($i = 0; $i < count($json_decoded->{'data'}); $i++) {
            $license_key = $json_decoded->{'data'}[$i]->{'license_key'};

}

echo $license_key;

3

Answers


  1. You probably have a white page because your script has an error (perhaps curl extension is not installed/enabled) and your PHP settings have error reporting disabled.

    You can try enabling error reports by adding this at top of your code:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    Or if that doesn’t work, find a php.ini file and enable errors:

    display_errors = on
    

    If you are confused about how to do any of this, you will have to search for how to enable error reporting for your hosting system and web server.

    Login or Signup to reply.
  2. For your JSON with the following format (which does NOT contain any square brackets in any levels)

    {"data": {"enabled": true,"product_link": "example","license_key": "BS7X4-55UH7-ZG2EV-ME2N5","buyer_email": "[email protected]","uses": 0,"date": "2022-11-11T20:56:37+00:00"}}
    

    You simply need $json_decoded->{'data'}->{'license_key'}; to get the license key

    So amend to

    <?php
    
    $url = "http://www.createchhk.com/SOanswers/subf/1.json";
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    $resp = curl_exec($curl);
    curl_close($curl);
    
    $json_decoded = json_decode($resp);
    
    $license_key = '';
    
    $license_key=$json_decoded->{'data'}->{'license_key'};
    
    echo $license_key;
    
    /*
    for ($i = 0; $i < count($json_decoded->{'data'}); $i++) {
                $license_key = $json_decoded->{'data'}[$i]->{'license_key'};
    }
    echo $license_key;
    */
    
    ?>
    

    You may visit the following sandbox link for the result:

    http://www.createchhk.com/SOanswers/subf/1.php

    Login or Signup to reply.
  3. Setup debugging in your development environment will help you best.

    1. First install the proper xdebug in your php installation
    2. Install the PHP Debug extension in you VSCode (I assume you use VSCode)
    3. Install a browser extention to activate the debugger. XDebug Helper for me
      Video

    In the browser activate debugging, in VSCode start the debugger. This way you can set breakpoints, read variables live and step through your code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search