1

I am trying to pull the top 6 artists from last.fm using the last.fm api. I am able to pull in the JSON data and output it just fine. However, when it comes to actually using specific pieces of data, I am at a loss. The only thing that I want to use is the artists' names. Here is the code I have so far.

<?php
    $content = get_data('http://ws.audioscrobbler.com/2.0/?method=chart.gettopartists&api_key=xxxxxxxxxxxxxx&format=json&limit=6');
    foreach ($content->artist as $artist) {
echo '<li>';
echo "{$artist->name}\n";
echo '</li>';
}
function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}           
?>

The data looks like this:

{"artists":{"artist":[{"name":"Coldplay","playcount":"757749","listeners":"111884","mbid":"cc197bad-dc9c-440d-a5b5-d52ba2e14234","url":"http:\/\/www.last.fm\/music\/Coldplay","streamable":"1","image":[{"#text":"http:\/\/userserve-ak.last.fm\/serve\/34\/210303.jpg","size":"small"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64\/210303.jpg","size":"medium"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/210303.jpg","size":"large"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/252\/210303.jpg","size":"extralarge"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/500\/210303\/Coldplay.jpg","size":"mega"}]},{"name":"Rihanna","playcount":"943551","listeners":"102321","mbid":"69989475-2971-49aa-8c53-5d74af88b8be","url":"http:\/\/www.last.fm\/music\/Rihanna","streamable":"1","image":[{"#text":"http:\/\/userserve-ak.last.fm\/serve\/34\/79835799.png","size":"small"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64\/79835799.png","size":"medium"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/79835799.png","size":"large"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/252\/79835799.png","size":"extralarge"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/_\/79835799\/Rihanna+PNG.png","size":"mega"}]}}}

Any help would be greatly appreciated!

Charles
  • 50,943
  • 13
  • 104
  • 142

1 Answers1

1

You need to use json_decode so return json_decode($data);. This will turn the return data in an object that you can traveres. For an associative array use return json_decode($data, true);. See http://php.net/manual/en/function.json-decode.php

qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • I made this change and now I receive this error: Warning: Invalid argument supplied for foreach() in /home/xptdbiyf/public_html/instagramTest.php on line 47. I'm just not sure how to access the specific pieces of data. – Chrystal Keane Dec 09 '12 at 04:56
  • `$content` would be the entire array so you are trying to access a variable that doesn't exist. The set of artists would be accessed by `$content->artists->artist` rather than `$content->artist`. – qooplmao Dec 09 '12 at 05:00
  • In the foreach loop you would treat each set as just `$artist` as set in the foreach so you would get the name by `$artist->name`. – qooplmao Dec 09 '12 at 05:07