1

I am retrieving artist information from the last.fm API. Here is a sample response for U2:

{
   "artist" : {
      "stats" : {
         "playcount" : "117896887",
         "listeners" : "3077194"
      },
      "name" : "U2",
      "tags" : {
         "tag" : [
            {
               "url" : "http://www.last.fm/tag/rock",
               "name" : "rock"
            },
            {
               "url" : "http://www.last.fm/tag/classic%20rock",
               "name" : "classic rock"
            },
            {
               "url" : "http://www.last.fm/tag/irish",
               "name" : "irish"
            },
            {
               "url" : "http://www.last.fm/tag/pop",
               "name" : "pop"
            },
            {
               "url" : "http://www.last.fm/tag/alternative",
               "name" : "alternative"
            }
         ]
      },
      "image" : [
         {
            "#text" : "http://userserve-ak.last.fm/serve/34/12369741.jpg",
            "size" : "small"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/64/12369741.jpg",
            "size" : "medium"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/126/12369741.jpg",
            "size" : "large"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/252/12369741.jpg",
            "size" : "extralarge"
         },
         {
            "#text" : "http://userserve-ak.last.fm/serve/_/12369741/U2.jpg",
            "size" : "mega"
         }
      ],
      "mbid" : "704acdbb-1415-4782-b0b6-0596b8c55e46",
      "similar" : {
         "artist" : [
            {
               "url" : "http://www.last.fm/music/Passengers",
               "name" : "Passengers",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/4826014.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/4826014.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/4826014.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/4826014.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/500/4826014/Passengers+Brian+Eno++U2+as++Du.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Bono",
               "name" : "Bono",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/2511353.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/2511353.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/2511353.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/2511353.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/2511353/Bono.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/R.E.M.",
               "name" : "R.E.M.",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/33444847.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/33444847.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/33444847.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/33444847.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/500/33444847/REM+11.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Coldplay",
               "name" : "Coldplay",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/28914.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/28914.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/28914.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/28914.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/28914/Coldplay.jpg",
                     "size" : "mega"
                  }
               ]
            },
            {
               "url" : "http://www.last.fm/music/Bruce+Springsteen",
               "name" : "Bruce Springsteen",
               "image" : [
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/34/2326566.jpg",
                     "size" : "small"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/64/2326566.jpg",
                     "size" : "medium"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/126/2326566.jpg",
                     "size" : "large"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/252/2326566.jpg",
                     "size" : "extralarge"
                  },
                  {
                     "#text" : "http://userserve-ak.last.fm/serve/_/2326566/Bruce+Springsteen+bruce.jpg",
                     "size" : "mega"
                  }
               ]
            }
         ]
      },
      "streamable" : "1",
      "bandmembers" : {
         "member" : [
            {
               "yearfrom" : "1976",
               "name" : "Bono"
            },
            {
               "yearfrom" : "1976",
               "name" : "The Edge"
            },
            {
               "yearfrom" : "1976",
               "name" : "Adam Clayton"
            },
            {
               "yearfrom" : "1976",
               "name" : "Larry Mullen, Jr."
            },
            {
               "yearfrom" : "1976",
               "name" : "Dick Evans"
            },
            {
               "yearfrom" : "1976",
               "name" : "Ivan McCormick"
            },
            {
               "yearfrom" : "1976",
               "name" : "Peter Martin"
            }
         ]
      },
      "url" : "http://www.last.fm/music/U2",
      "ontour" : "0",
      "bio" : {
         "published" : "Tue, 27 Sep 2011 15:29:59 +0000",
         "yearformed" : "1976",
         "summary" : "\n U2 is an Irish rock band which formed in 1976 in Dublin, Ireland. Since the band’s formation, they have consisted of Bono</a> (real name Paul Hewson) (vocals, guitar, harmonica), The Edge</a> (real name David Evans) (guitar, keyboards, backing vocals), Adam Clayton</a> (bass) and Larry Mullen, Jr.</a> (drums, percussion). The band is the biggest selling alternative rock act of all time, having sold 140 million albums worldwide as of 2008. They have won 22 Grammys and were inducted into the Rock & Roll Hall of Fame in 2005. As of 2009, they have released 12 albums and 58 singles. \n\n Read more about U2 on Last.fm</a>.\n \n ",
         "placeformed" : "Dublin, Ireland",
         "formationlist" : {
            "formation" : {
               "yearfrom" : "1976",
               "yearto" : ""
            }
         },
         "content" : "\n U2 is an Irish rock band which formed in 1976 in Dublin, Ireland. Since the band’s formation, they have consisted of Bono</a> (real name Paul Hewson) (vocals, guitar, harmonica), The Edge</a> (real name David Evans) (guitar, keyboards, backing vocals), Adam Clayton</a> (bass) and Larry Mullen, Jr.</a> (drums, percussion). The band is the biggest selling alternative rock act of all time, having sold 140 million albums worldwide as of 2008. They have won 22 Grammys and were inducted into the Rock & Roll Hall of Fame in 2005. As of 2009, they have released 12 albums and 58 singles. \n\n Read more about U2 on Last.fm</a>.\n \n \nUser-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.\n ",
         "links" : {
            "link" : {
               "rel" : "original",
               "href" : "http://www.last.fm/music/U2/+wiki",
               "#text" : ""
            }
         }
      }
   }
}

I want to display the summary only but I'm having trouble selecting it in my script. I've tried the following

 echo $result->artist->bio->summary; 

or

$output = $result->artist->bio->summary;
echo '<ul>';
foreach ($bands as $artist) {
            $name  ->name;
                    etc

}
if ($result === NULL) die('Artist not found');

If somebody could show me how to select and display one object from the returned JSON that would be great. I've tried everything I can think of or find

enter code hereobject(stdClass)[1]

public 'artist' => 
    object(stdClass)[2]
      public 'name' => string 'Foo Fighters' (length=12)
      public 'mbid' => string '67f66c07-6e61-4026-ade5-7e782fad3a5d' (length=36)
      public 'bandmembers' => 
        object(stdClass)[3]
          public 'member' => 
            array (size=7)
              ...
      public 'url' => string 'http://www.last.fm/music/Foo+Fighters' (length=37)
      public 'image' => 
        array (size=5)
          0 => 
            object(stdClass)[11]
              ...
          1 => 
            object(stdClass)[12]
              ...
          2 => 
            object(stdClass)[13]
              ...
          3 => 
            object(stdClass)[14]
              ...
          4 => 
            object(stdClass)[15]
              ...
      public 'streamable' => string '1' (length=1)
      public 'ontour' => string '0' (length=1)
      public 'stats' => 
        object(stdClass)[16]
          public 'listeners' => string '3589242' (length=7)
          public 'playcount' => string '155892081' (length=9)
      public 'similar' => 
        object(stdClass)[17]
          public 'artist' => 
            array (size=5)
              ...
      public 'tags' => 
        object(stdClass)[48]
          public 'tag' => 
            array (size=5)
              ...
      public 'bio' => 
        object(stdClass)[54]
          public 'links' => 
            object(stdClass)[55]
              ...
          public 'published' => string 'Fri, 9 Nov 2012 21:02:30 +0000' (length=30)
          public 'summary' => string '
                <a href="http://www.foofighters.com" rel="nofollow">Foo Fighters</a> are an American <a href="http://www.last.fm/tag/rock" class="bbcode_tag" rel="tag">rock</a> band formed by singer/guitarist/drummer <a href="http://www.last.fm/music/Dave+Grohl" class="bbcode_artist">Dave Grohl</a> in 1995 in Seattle, USA. Grohl formed the group as a one-man project after the dissolution of his previous band <a href="http://www.last.fm/music/Nirvana" class="bbcode_artist">Nirvana</a> in 1994. Prior to the '... (length=1384)
          public 'content' => string '
                    <a href="http://www.foofighters.com" rel="nofollow">Foo Fighters</a> are an American <a href="http://www.last.fm/tag/rock" class="bbcode_tag" rel="tag">rock</a> band formed by singer/guitarist/drummer <a href="http://www.last.fm/music/Dave+Grohl" class="bbcode_artist">Dave Grohl</a> in 1995 in Seattle, USA. Grohl formed the group as a one-man project after the dissolution of his previous band <a href="http://www.last.fm/music/Nirvana" class="bbcode_artist">Nirvana</a> in 1994. Prior to '... (length=1514)
          public 'placeformed' => string 'Seattle, USA' (length=12)
          public 'yearformed' => string '1995' (length=4)
          public 'formationlist' => 
            object(stdClass)[57]
              ...
  • 1
    Have you decoded the JSON with `json_decode` yet? Try `$json = json_decode($response);` and then `print_r($json)` will show you the structure of what you are working with. – halfer Jan 04 '14 at 17:24
  • The array structure is now available above, any suggestions? – First_Explorer Jan 04 '14 at 17:44
  • I can't see an array structure above, just a JSON string. Did you try the function I suggested? – halfer Jan 04 '14 at 17:46
  • sorry my bad, I got an Undefined variable: for $json. $json = json_decode($result); print_r($json); – First_Explorer Jan 04 '14 at 17:58
  • Whatever variable you are using to store the JSON string, feed it into that function. I used `$response` in my earlier comment, but I _think_ it might be `$result` in your code. If you get an undefined variable, debug. – halfer Jan 04 '14 at 18:43
  • thanks for the tips halfer, but still no joy. – First_Explorer Jan 05 '14 at 09:26
  • 1
    I would recommend doing some study on the basics then, to be honest. If you are new to PHP then you'll really struggle with strings, arrays, objects, types and loops (and the rest of it!) unless you've worked through a book and got a basic working knowledge. Have a look at the [Codecademy PHP series](http://www.codecademy.com/tracks/php) - I'm currently recommending them for my students. – halfer Jan 05 '14 at 09:33
  • Thanks Halfer, I made it to about 70% of the way through the code academy tutorials, but time was against me so just had to give it a go as I have a submission to make early next week bbut i'll get there. – First_Explorer Jan 05 '14 at 09:53
  • @First_explorer do you still need help with that I added the answer... – meda Feb 18 '14 at 12:49
  • Thanks meda, I managed to figure that one out, lately all my problems are android related – First_Explorer Feb 18 '14 at 13:06

1 Answers1

0

Here is how you would parse json data:

PHP

$decodedJson = json_decode($json);

$artist = $decodedJson->{"artist"};
$bio = $artist->{"bio"};
$summary = $bio->{"summary"};
var_dump($summary);

Program Output

string(631) " U2 is an Irish rock band which formed in 1976 in Dublin, Ireland. Since the band’s formation, they have consisted of Bono (real name Paul Hewson) (vocals, guitar, harmonica), The Edge (real name David Evans) (guitar, keyboards, backing vocals), Adam Clayton (bass) and Larry Mullen, Jr. (drums, percussion). The band is the biggest selling alternative rock act of all time, having sold 140 million albums worldwide as of 2008. They have won 22 Grammys and were inducted into the Rock & Roll Hall of Fame in 2005. As of 2009, they have released 12 albums and 58 singles.

Read more about U2 on Last.fm. "

Demo

meda
  • 45,103
  • 14
  • 92
  • 122