j3j5/lastfm-apio

一个简单的 Last.fm API PHP 包装器,允许您启动并行请求。

dev-master 2015-12-27 17:44 UTC

This package is auto-updated.

Last update: 2024-09-16 11:46:40 UTC


README

Build Status

LastfmApio

LastfmApio 是一个简单的 Last.fm API PHP 包装器,允许您进行并行请求,从而使其运行速度更快(类似 JS 风格)。

内部,它使用 marcushat/RollingCurlX,它是 cURL Multi 的包装器。

安装

j3j5/lastfm-apio 添加到 composer.json

"j3j5/lastfm-apio": "dev-master"

运行 composer update 以获取 LastfmApio 的最新版本。

或者,您也可以运行

$ composer require j3j5/lastfm-apio dev-master

配置

打开与包一起提供的 config.php 文件,并在此处设置所有 API 密钥和密钥(可选)。

或者,您还可以设置自己的配置数组,并在创建 LastfmApio 的第一个实例时使用它来覆盖配置文件。数组配置必须如下所示

$lastfm_settings = array(
	'api_key'		=> 'YOUR_API_KEY',
	'api_secret'	=> 'YOUR_API_SECRET',
);

$api = new LastfmApio($lastfm_settings);

使用

一旦您创建了库的自己的实例,您就可以使用任何公共方法来请求 Twitter 的 API。

如果您决定从您自己的应用程序而不是从配置文件设置您的令牌

use j3j5\LastfmApio;

$lastfm_settings = array(
	'api_key'		=> 'YOUR_API_KEY',
	'api_secret'	=> 'YOUR_API_SECRET',
);

$api = new LastfmApio($lastfm_settings);

// Now you can do all type of requests

$user_info = $api->user_getinfo(
	array('user' => $username)
);
$artist = $api->artist_getInfo(
	array('artist' => 'Rosendo')
);

或者更有趣的……具有并发请求的!

use j3j5\LastfmApio;

$lastfm_settings = array(
	'api_key'		=> 'YOUR_API_KEY',
	'api_secret'	=> 'YOUR_API_SECRET',
);

$api = new LastfmApio($lastfm_settings);

$username = 'lapegatina';
$api->user_getweeklyartistchart(array('user' => $username), FALSE, TRUE);
$api->user_getweeklyartistchart(array('user' => $username, 'from' => 1210507200, 'to' => 1211112000), FALSE, TRUE);
$api->user_getweeklyartistchart(array('user' => $username, 'from' => 1217764800, 'to' => 1218369600), FALSE, TRUE);
$api->user_getweeklyartistchart(array('user' => $username, 'from' => 1232280000, 'to' => 1232884800), FALSE, TRUE);

// The response to all concurrent requests is return as an array using as key a string of the parameters joined by '.'
$responses = $api->run_multi_requests();
foreach($responses AS $response) {
	$allresponseresults = isset($response->weeklyartistchart->artist) ? $response->weeklyartistchart->artist : array();
	print count($allresponseresults) . " artists found." . PHP_EOL;
}

您还可以更改针对 API 启动的最大并发请求数量,方法是

$api->set_max_concurrent_reqs(50); // High values might get you in trouble with Last.fm, please be considerate with them!

这个库深受 dandelionmood/php-lastfm 的启发,它很有用,但不受多请求支持,这促使我编写了这个。