How do I remove some data from a JSON and display only the rest?

0

Hello, I want to print some data from a JSON page in PHP, for example:

I have this page here link

JSON:

{
    "totalstreams": 1,
    "activestreams": 1,
    "currentlisteners": 19,
    "peaklisteners": 57,
    "maxlisteners": 500,
    "uniquelisteners": 18,
    "averagetime": 881,
    "version": "2.2.1.109 (posix(linux x64))",
    "streams": [{
        "id": 1,
        "currentlisteners": 19,
        "peaklisteners": 57,
        "maxlisteners": 500,
        "uniquelisteners": 18,
        "averagetime": 881,
        "servergenre": "� s� granada na cara da concorrencia",
        "serverurl": "http:\/\/www.habbonados.com.br",
        "servertitle": "gabrielsa441",
        "songtitle": "Wiz Khalifa - See You Again ft. Charlie Puth [Official Video] Furious 7 Soundtrack",
        "irc": "https:\/\/www.habbo.com.br\/hotel?room=81601237",
        "icq": "NA",
        "aim": "NA",
        "streamhits": 2493,
        "streamstatus": 1,
        "backupstatus": 0,
        "streampath": "\/",
        "streamuptime": 645,
        "bitrate": 64,
        "content": "audio\/aacp"
    }]
}

I want to get this data out of it:

"uniquelisteners":10,
"averagetime":462,
"servergenre":"� s� granada na cara da concorrencia"

And move to a PHP page with echo

    
asked by anonymous 15.06.2016 / 17:05

2 answers

1

The most direct way would be this:

<?php
$url = 'http://hts03.kshost.com.br:8642/statistics?json=1';
$dados = json_decode(file_get_contents($url));

Now the variable "$ data" has all the JSON information, you type as you want on the screen.

    
15.06.2016 / 17:30
5

Look, so there was a problem with character encoding , we have to do utf8_encoding :

<?php
$jsonRaw = file_get_contents('http://hts03.kshost.com.br:8642/statistics?json=1');
$jsonParsed = json_decode(utf8_encode($jsonRaw), true);

echo($jsonParsed['uniquelisteners']). '<br>';
echo($jsonParsed['averagetime']). '<br>';
echo($jsonParsed['streams'][0]['servergenre']). '<br>';

Here's a solution using curl :

<?php
function get_data($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
    $return = curl_exec($curl);
    curl_close($curl);
    return $return;
}

$jsonRaw = get_data('http://hts03.kshost.com.br:8642/statistics?json=1');
$jsonParsed = json_decode(utf8_encode($jsonRaw), true);

echo($jsonParsed['uniquelisteners']). '<br>';
echo($jsonParsed['averagetime']). '<br>';
echo($jsonParsed['streams'][0]['servergenre']). '<br>';
    
15.06.2016 / 17:42