skip to Main Content

I have a web page in json format, whose url is: http://radiojoven.6te.net/teste/now.json.

I’ve been researching ways to extract the information I want, using echo php. I made a code, but I’m not able to extract the information I want, which is "song_name": "MAKIN’ WHOOPEE","artist_name": "YANNICK BOVY".

Attached, I show the code so that you can tell me where my error is:

<?php
    $content = file_get_contents("http://radiojoven.6te.net/teste/now.json");
    $content = utf8_encode($content);
    $result = json_decode($content, true);

    $url = $result['zenon']['song_name'];
    
    echo $url;
?>

<?php
    $content = file_get_contents("http://radiojoven.6te.net/teste/now.json");
    $content = utf8_encode($content);
    $result = json_decode($content, true);

    $url = $result['zenon']['artist_name'];
    
    echo $url;
?>

Is there any way to remove the remaining "song_name" and "artist_name", I try to use [‘artist_name’][1] (…) with no result.

Thank you very much for your attention and help.

3

Answers


  1. its the other way round?

    you have to [1][‘artist_name’] instead of [‘artist_name’][1]?

    because you have a list of played entries each has a artist_name

    the correct access would be:

    $url = $result['now_playing_record'][0]['zenon']['artist_name'];
    
    Login or Signup to reply.
  2. I would advise you to use Curl instead of file_get_contents.

    If you want to keep using file_get_contents this is the way to do it:

        $content = file_get_contents("http://radiojoven.6te.net/teste/now.json");
        $content = utf8_encode($content);
        $result = json_decode($content, true);
        $currentSongName = $result["now_playing_record"][0]['zenon']['song_name'];
        $currentArtistName = $result["now_playing_record"][0]['zenon']['artist_name'];
    
        echo $currentArtistName . " | " . $currentSongName;
    

    In case you want to switch to Curl you can use this

        $url = "http://radiojoven.6te.net/teste/now.json";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result, true);
        $result = json_decode(json_encode($result), FALSE);
    
        $currentSongName = $result->now_playing_record[0]->zenon->song_name;
        $currentArtistName = $result->now_playing_record[0]->zenon->artist_name;
    
        echo $currentArtistName . " | " . $currentSongName;
    

    Note: The code is not checking if there is no song playing at all.

    Login or Signup to reply.
  3. It seems you are trying to access the values using invalid keys. You’re attempting to access $result['zenon']['song_name'] but the correct path to access these values is $result['now_playing_record'][0]['zenon']['song_name'] for the first record in the array. Try this

    $content = file_get_contents("http://radiojoven.6te.net/teste/now.json");
    $content = utf8_encode($content);
    $result = json_decode($content, true);
    
    // Check if 'now_playing_record' exists and is an array
    if (isset($result['now_playing_record']) && is_array($result['now_playing_record'])) {
        // Loop through each record in 'now_playing_record'
        foreach ($result['now_playing_record'] as $record) {
            // Check if 'zenon' exists within the record
            if (isset($record['zenon'])) {
                $songName = $record['zenon']['song_name'];
                $artistName = $record['zenon']['artist_name'];
                echo "Song Name: " . $songName . "<br>";
                echo "Artist Name: " . $artistName . "<br>";
            }
        }
    } else {
        echo "Unable to fetch song and artist information.";
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search