简体   繁体   English

last.fm API分页

[英]last.fm API pagination

I'm creating a last.fm/ google maps mashup, plotting gig map markers for whatever city you search for. 我正在创建一个last.fm/谷歌地图混搭程序,为要搜索的城市绘制演出地图标记。

A last.fm API response is like this: last.fm API响应如下:

<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. 例如,目前您可以搜索爱丁堡-它将带回即将到来的前10场演出。 I'm using PHP and simple XML to return the results. 我正在使用PHP和简单的XML返回结果。

I want to add pagination - so first 10 gigs would be page 1; 我想添加分页-所以前10个演出将是第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. 有很多有用的资源可用于mysql / php分页,但对于动态PHP / XML和API则不多。 What would be the best way to approach this? 解决此问题的最佳方法是什么?

The last.fm API allows for parameters, including page number. last.fm API允许使用参数,包括页码。 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 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. 您可以在PHP代码中使用$ _GET传递页码,并相应地更新请求URL。

$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. 这显然是一个非常基本的示例,没有对GET参数等进行任何类型的错误处理,请不要在生产中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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