0

Update:

Thanks Rambo for the great answer. The only issue that I have now is that it only displays artist information so long as the artists next gig is in the UK. For example, if they're playing in France and THEN the UK - it won't display anything (Or it will display my else message). If their next gig IS in the UK, then it will echo artist information etc. Any idea how to get it to echo only UK information, regardless if they're in another country before hand?

Thank you.

Original Post:

I'm currently creating a website for my final major project. I retrieve data using the Last.fm API using PHP and XML. It's going well so far, but there are a few issues I'm having trouble with. I'm very new to PHP, so I want to use this opportunity to develop some skills.

  1. I want to limit the data to my city or country.
  2. How do I retrieve images from an XML document?

Using the last.fm API, more specifically, the artist.getEvents (http://bit.ly/zYzWo6) - I am able to create a basic search field so that the user can type in an artist name. This is a great step in the right direction, but the problem is, any results outside of my country is irrelevant for my project. Using artist.getEvents doesn't allow any specific parameters such as location - geo.getEvents (http://bit.ly/wpSQwd) does however.

The following is the code used for my basic search:

<?php

    $first_bit_of_url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=';
    $last_bit_of_url = '&api_key=b25b959554ed76058ac220b7b2e0a026&d';


    $artist = ($_GET["artist"]); // gets the information passed by the input form

    $query_url = $first_bit_of_url . $artist . $last_bit_of_url ;


    $upcoming_gig_data_xml = simplexml_load_file($query_url);

    $search_result = $upcoming_gig_data_xml->events->event->artists->artist;
    $venue_result = $upcoming_gig_data_xml->events->event->venue->name;
    $city_result = $upcoming_gig_data_xml->events->event->venue->location->city;

    for ($i = 0; $i < 5; $i++){

    echo $search_result . "<br />";
    echo $venue_result . ", ";
    echo $city_result . "<br />";

    } ?>

Secondly how would I go about retrieving an image from, for example, this sample of XML code used in the above context? I've briefly read some articles on Xpath, can I mix the Xpath method with the method I'm using above?

<image size="small">...</image>
  <image size="medium">...</image>
  <image size="large">...</image>

Hopefully someone can point me in the right direction here, and I appreciate any help given.

Thanks,

Chris.

3 Answers3

0

for your second question: use simple xml http://www.php.net/manual/de/ref.simplexml.php

for example your xml might be:

<?xml version="1.0" encoding="UTF-8"?>
<images>
  <image size="small">1</image>
  <image size="medium">2</image>
  <image size="large">3</image>
</images>

load the xml file with simple xml and access the nodes like this. This is just a simple example.

$r = simplexml_load_file('test.xml');
foreach($r->image as $img) {
  print $img . ' and size is ' . $img['size'] . "<br/>";
}
busypeoples
  • 737
  • 3
  • 6
0

$smallimg = $upcoming_gig_data_xml->events->event->venue->image['small'];

then you can echo the image out using the img html tag, giving it's src the value of $smallimg

I haven't tested this, but hopefully it'll get you in the right direction

update

for the first point, loop through the xml file and do a match for the country so if it is equal to the united kingdom then process everything, otherwise it will skip it

foreach($upcoming_gig_data_xml->events->event as $event)
{
   if($event->location == "United Kingdom")
   {
      // process everything here
   }
}
Rambo
  • 248
  • 4
  • 14
0

You could use XPath to get only the event elements with UK venues.

$lfm = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=metallica&api_key=b25b959554ed76058ac220b7b2e0a026&d');
$uk_events = $lfm->xpath('events/event[venue/location/country="United Kingdom"]');
foreach ($uk_events as $event) {
    $venue_city = (string) $event->venue->location->city;
    $large_pics = array_map('strval', $event->xpath('image[@size="large"]'));
    // Do whatever other processing/displaying you like…
}

(See it running.)

salathe
  • 51,324
  • 12
  • 104
  • 132