Jump to content

Recommended Posts

Hi,

 

I've got a test page up at http://ronniebrown.co.uk/parse_freshmeat.php.  THis page is pulling in XML from Last.fm - http://ws.audioscrobbler.com/1.0/user/ronniebrown/recenttracks.xml and I have PHP written to render it on screen.

 

My problem is that only the "artist" is being brought back; neither of the other two elements (url, track) are rendered on screen.

 

My PHP code is below.  Any help much appreciated.

 

<?php
if( ! ($fp = fopen("http://ws.audioscrobbler.com/1.0/user/ronniebrown/recenttracks.xml" , "r" )) )
  die("Couldn't open xml file!");
$item_counter = 0;
$in_item_tag = 0;
$fm_current_tag_state = '';
$fm_headline_data = array();
function startElementHandler( $parser, $element_name, $element_attribs )
{
  global $item_counter;
  global $in_item_tag;
  global $fm_current_tag_state;
  global $fm_headline_data;
  if( $element_name == "TRACK" )
  {
    $in_item_tag = 1;
  }
  if( $in_item_tag == 1 )
  {
    $fm_current_tag_state = $element_name;
  }
  else
  {
    $fm_current_tag_state = '';
  }
}
function endElementHandler( $parser, $element_name )
{
  global $item_counter;
  global $in_item_tag;
  global $fm_current_tag_state;
  global $fm_headline_data;
  $fm_current_tag_state = '';
  if( $element_name == "TRACK" )
  {
    $item_counter++;
    $in_item_tag = 0;
  }
}
function characterDataHandler( $parser , $data )
{
  global $item_counter;
  global $in_item_tag;
  global $fm_current_tag_state;
  global $fm_headline_data;
  if( $fm_current_tag_state == '' || $in_item_tag == 0 )
    return;
  if( $fm_current_tag_state == "URL" ) {
    $fm_headline_data[$item_counter]["url"] = $data;
  }
  if( $fm_current_tag_state == "TRACK" ) {
    $fm_headline_data[$item_counter]["track"] = $data;
  }
  if( $fm_current_tag_state == "ARTIST" ) {
    $fm_headline_data[$item_counter]["artist"] = $data;
  }
}
if( !($xml_parser = xml_parser_create()) )
  die("Couldn't create XML parser!");

xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler( $xml_parser , "characterDataHandler" );
while( $data = fread($fp, 4096) )
{
  if( !xml_parse($xml_parser, $data, feof($fp)) )
  {
    break; // get out of while loop if we're done with the file
  }
}
xml_parser_free($xml_parser);
?>
<HTML>
<HEAD>
<TITLE>Music</TITLE>
</HEAD>

<BODY BGCOLOR="#ffffff">

<H3>Music</H3>
<BR>
<?php

for( $i=0 ; $i < $item_counter ; ++$i )
{
  printf("<A HREF=\"%s\">%s</a> - %s<br>\n" , $fm_headline_data[$i]["url"] , 
                                              $fm_headline_data[$i]["track"] ,
                                              $fm_headline_data[$i]["artist"] );
}

?>

</BODY>
</HTML>

Link to comment
https://forums.phpfreaks.com/topic/86847-php-xml-help/
Share on other sites

Hi Daniel,

 

Thanks for the prompt reply.

 

I took a look at the tutorial and came up with this page, which seems to reach the data ok but doesn't seem to loop through and return the results:

 

http://ronniebrown.co.uk/test.php

 

This is the (slightly) changed code:

 

<?php
$rss = new SimpleXMLElement('http://ws.audioscrobbler.com/1.0/user/ronniebrown/recenttracks.xml', null, true);

echo "<h1><a href='{$rss->track->artist}'>{$rss->track->artist}</a></h1>".PHP_EOL.'<hr />'.PHP_EOL;

foreach($rss->xpath('track/artist') as $item)
{
    echo <<<EOF
<h2><a href='{$item->url}'>{$item->title}</a></h2>
<div>Posted at: {$item->date}</div>
{$item->track}
<hr />

EOF;
}
?>

 

What am I doing wrong?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/86847-php-xml-help/#findComment-443881
Share on other sites

Try

<?php
$tracks = new SimpleXMLElement('http://ws.audioscrobbler.com/1.0/user/ronniebrown/recenttracks.xml', null, true);

foreach($tracks as $track)
{
    echo <<<EOF
Name: {$track->name}<br />
Artist: {$track->artist}<br />
<a href='{$track->url}'>{$track->url}</a>
<hr />

EOF;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/86847-php-xml-help/#findComment-444193
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.