Home > Enterprise >  How I can get specific data from API
How I can get specific data from API

Time:11-12

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;

CodePudding user response:

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.

CodePudding user response:

For your JSON , 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

  • Related