last.fm API pagination

五迷三道 提交于 2019-12-11 13:53:32

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4846040/last-fm-api-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!