简体   繁体   中英

last.fm API pagination

I'm creating a last.fm/ google maps mashup, plotting gig map markers for whatever city you search for.

A last.fm API response is like this:

<events location="New York" page="1" totalpages="105" total="1050">
<event>
  <id>640418</id>
  <title>Nikka Costa</title>
  <artists> etc.

Currently, you can search for Edinburgh, for example - and it will bring back the first 10 gigs coming soon. I'm using PHP and simple XML to return the results.

I want to add pagination - so first 10 gigs would be page 1; I would like to have a list of other pages so that users can look forward for later gigs.

There are many useful resources out there for mysql/php pagination but not so much for dynamic PHP/XML and API. What would be the best way to approach this?

The last.fm API allows for parameters, including page number. So you would structure your request something like this:

http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=new+york&api_key= api key &page=2

You could pass the page number using $_GET in your PHP code and update the request URL accordingly.

$page = 1;
if (isset($_GET['lfm_pageid'])) {
    $page = $_GET['lfm_pageid'];
}

$lfm_events = new SimpleXMLElement("http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=new+york&api_key=b25b959554ed76058ac220b7b2e0a026&page=$page", NULL, TRUE);

foreach ($lfm_events->events->event as $event) {
    echo $event->title . '<br />';
}

This is obviously a VERY basic example without any kind of error handling for GET parameters etc, don't use it in production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM