last.fm API分页 [英] last.fm API pagination

查看:157
本文介绍了last.fm API分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个last.fm/谷歌地图混搭程序,为您要搜索的城市绘制演出地图标记.

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

last.fm API响应如下:

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.

例如,当前您可以搜索爱丁堡-它将带回即将到来的前10场演出.我正在使用PHP和简单的XML返回结果.

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.

我想添加分页-所以前10个演出将是第1页;我希望有其他页面的列表,以便用户可以期待以后的演出.

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.

有很多有用的资源可用于mysql/php分页,但对于动态PHP/XML和API则不多.最好的方法是什么?

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?

推荐答案

last.fm API允许使用参数,包括页码.因此,您可以按以下方式组织您的请求:

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

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

您可以在PHP代码中使用$ _GET传递页码,并相应地更新请求URL.

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 />';
}

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

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

这篇关于last.fm API分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆