MultiformeIngegno
MultiformeIngegno

Reputation: 7059

Decoding JSON input in PHP

<?php
$json = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=XXXXX&api_key=XXXX&format=json');
$track_data = json_decode($json);


    foreach ($track_data as $data)
    {
        if (!empty($data)) {
            $text = $data->recenttracks->track->artist->mbid;
        }
        echo '<span>'; echo $text; echo '</span>';
    }
?>

This is the code I'm trying to use. The problem is: nothing shows up... What's wrong with it? :) Here's the JSON input (the content of the URL on file_get_contents): http://pastebin.com/NLHGMapT

Upvotes: 0

Views: 305

Answers (1)

Ziarno
Ziarno

Reputation: 7572

Track is an array:

$text = $data->recenttracks->track[0]->artist->mbid;

also, when you're in the foreach loop you don't need to use recenttracks:

$text = $data->track[0]->artist->mbid;

Upvotes: 1

Related Questions