Jump to content

Pull the top 20 from last.fm


gesseg

Recommended Posts

Hi I need to pull the top 20 from my last.fm account. Last.fm provide an API key (?) Heres the link to mine http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=sandi-g&api_key=b25b959554ed76058ac220b7b2e0a026 I need to search this string (as html code not what a browser outputs) for "<name>" and then return all character untill the next "<" the search on again for the next "<name>" all the way up to 20.

 

Ive not got very far on this one to be honest. I dont even know how to search it as plain txt. I dont necessarily want some one to do it for me but bare in mind Ive only been coding for a few days.

Link to comment
Share on other sites

Without looking too much in to it (a simple Google query for "last.fm API tutorial" would probably solve all of your problems), I assume the API returns XML.  You might want to look up the SimpleXML library in PHP.

Link to comment
Share on other sites

sorry, it does return xml. Ive had a play round with parsing the xml. and ive got the script working but I dont understand enought to make it do what i want  :shrug:

 

I want it to only output the </name> tags. Anyway heres the code i have:

<?php 

$file = "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=sandi-g&12month&api_key=b25b959554ed76058ac220b7b2e0a026"; 

function contents($parser, $data){ 
    echo $data; 
} 

function startTag($parser, $data){ 
    echo "<b>"; 
} 

function endTag($parser, $data){ 
    echo "</b><br />"; 
} 

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents"); 

$fp = fopen($file, "r"); 

$data = fread($fp, 3368500); 

if(!(xml_parse($xml_parser, $data, feof($fp)))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser)); 
} 

xml_parser_free($xml_parser); 

fclose($fp); 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.