PHP Echo Nest API

A simple, Object Oriented API wrapper for the EchoNest Api written with PHP5. This library is modeled after the php-github-api library built by ornicar

Uses EchoNest API v4.

Requires

  • PHP 5.2 or 5.3.
  • php curl but it is possible to write another transport layer..

If the method you need does not exist yet, dont hesitate to request it with an issue!

Autoload

The first step to use php-echonest-api is to register its autoloader:

require_once '/path/to/lib/EchoNest/Autoloader.php';
EchoNest_Autoloader::register();

Replace the /path/to/lib/ path with the path you used for php-echonest-api installation.

php-echonest-api follows the PEAR convention names for its classes, which means you can easily integrate php-echonest-api classes loading in your own autoloader.

Instanciate a new EchoNest Client

$echonest = new EchoNest_Client();

From this object you can now access all of the different EchoNest APIs (listed below)

Authenticate a user

Authenticate using your EchoNest API Key. You can obtain one at EchoNest by Registering an Account

$echonest->authenticate($apiKey);

Deauthenticate a user

Cancels authentication.

$echonest->deAuthenticate();

Next requests will not be authenticated

Artists

For searching artists, getting artist information and music. Wraps EchoNest Artist API.

$artistApi = $echonest->getArtistApi();

Search for artists by name

$results = $echonest->getArtistApi()->search(array('name' => 'Radiohead'));
print_r($results);

  Array
  (
      [0] => Array
          (
              [name] => Radiohead
              [id] => ARH6W4X1187B99274F
          )

  )

Returns an array of results as described in http://developer.echonest.com/docs/v4/artist.html#search

Get information about an artist

$bios = $echonest->getArtistApi()->setName('Radiohead')->getBiographies();

Once you set an artists name or id on an artist API, the API will remember that artist and use them for future function calls

$artistApi = $echonest->getArtistApi();
$artistApi->setName('Radiohead');
$bios   = $artistApi->getBiographies();
$audio  = $artistApi->getAudio();
$images = $artistApi->getImages();

Each function comes with a variety of options. Please view the documentation in this project or on http://echonest.com to see all the options available

Songs

Api calls for getting data about songs. Wraps EchoNest Song API.

$songApi = $echonest->getSongApi();

Please view the documentation in this project or on http://echonest.com to see all the options available

Playlists

Api calls for generating playlists. Wraps EchoNest Playlist API.

$playlistApi = $echonest->getPlaylistApi();

Please view the documentation in this project or on http://echonest.com to see all the options available

Catalogs

API calls for managing personal catalogs. Wraps EchoNest Catalog API.

$catalogApi = $echonest->getCatalogApi();

Please view the documentation in this project or on http://echonest.com to see all the options available

Tracks

Methods for analyzing or getting info about tracks. Wraps EchoNest Track API.

$trackApi = $echonest->getTrackApi();

Please view the documentation in this project or on http://echonest.com to see all the options available

The Response

The API tries to return to you the information you typically need, and spare you information such as status codes, messages, etc, if the response was successful. Below is an example of a fully rendered response.

  Array
  (
      [status] => Array
          (
              [version] => 4.2
              [code] => 0
              [message] => Success
          )

      [artists] => Array
          (
              [0] => Array
                  (
                      [name] => Radiohead
                      [id] => ARH6W4X1187B99274F
                  )

          )

  )

Often times, the status information is not needed. However, if you would like the API to return the full response (this is often needed when dealing with pagers, as the "total" and "start" parameters are passed outside of the "artists" array, for example), set the raw option to true for your API.

// pass options to getter
$response = $echonest->getArtistApi(array('raw' => true))->search(array('name' => 'Radiohead'));

// set options manually
$artistApi = $echonest->getArtistApi();
$artistApi->setOption('raw', true);
$response = $artistApi->search(array('name' => 'Radiohead'));

If you think this is dumb, let me know and I will consider making raw the default.

Sandbox

Set up the sandbox api to begin with

$sandboxApi = $echonest->getSandboxApi()->setOAuthConfig(array(
  "consumer_key" => $consumer_key,
  "consumer_secret" => $consumer_secret
))->setSandbox($sandbox_key);

Fetch an array of all available assets

$assets = $sandboxApi->assets($start, $per_page = 100);

Access an individual asset

$sandboxApi->access($id)

Access an array of assets

$sandboxApi->access($array)

To Do

Better documentation and test coverage will be coming soon



PHP Echo Nest API

用PHP5编写的EchoNest Api简单的面向对象的API包装器。 该库是在由 php-github-api 库之后建模的.com / ornicar> orn​​icar

使用 EchoNest API v4

需要

  • PHP 5.2或5.3。
  • php curl ,但可以编写另一个传输层。

如果您需要的方法不存在,请不要犹豫,请问题

自动加载

使用php-echonest-api的第一步是注册其自动加载器:

require_once '/path/to/lib/EchoNest/Autoloader.php';
EchoNest_Autoloader::register();

/ path / to / lib / 路径替换为用于php-echonest-api安装的路径。

php-echonest-api follows the PEAR convention names for its classes, which means you can easily integrate php-echonest-api classes loading in your own autoloader.

修改一个新的EchoNest客户端

$echonest = new EchoNest_Client();

从此对象可以访问所有不同的EchoNest API(如下所示)

验证用户

使用您的EchoNest API密钥进行身份验证。您可以通过注册帐户在EchoNest获取一个。

$echonest->authenticate($apiKey);

对用户进行身份验证

取消认证。

$echonest->deAuthenticate();

下一个请求将不会被认证

艺术家

搜索艺术家,获取艺术家信息和音乐。 包裹 EchoNest Artist API

$artistApi = $echonest->getArtistApi();

按名称搜索艺术家

$results = $echonest->getArtistApi()->search(array('name' => 'Radiohead'));
print_r($results);

Array ( [0] => Array ( [name] => Radiohead [id] => ARH6W4X1187B99274F )

)

返回一组结果,如 http://developer.echonest.com/docs/v4中所述/artist.html#search

获取有关艺术家的信息

$bios = $echonest->getArtistApi()->setName('Radiohead')->getBiographies();

在艺术家API上设置艺术家名称或编号后,API会记住该艺术家并将其用于将来的功能调用。

$artistApi = $echonest->getArtistApi();
$artistApi->setName('Radiohead');
$bios   = $artistApi->getBiographies();
$audio  = $artistApi->getAudio();
$images = $artistApi->getImages();
每个功能都有各种选项。请查看此项目中的文档或 http://echonest.com 查看所有可用选项

歌曲

Api要获取有关歌曲的数据。 包裹 EchoNest歌曲API

$songApi = $echonest->getSongApi();

请查看此项目中的文档或 http://echonest.com 查看所有可用选项

播放列表

Api调用生成播放列表。 包裹 EchoNest播放列表API

$playlistApi = $echonest->getPlaylistApi();

请查看此项目中的文档或 http://echonest.com 查看所有可用选项

目录

用于管理个人目录的API调用。 包裹 EchoNest Catalog API

$catalogApi = $echonest->getCatalogApi();

请查看此项目中的文档或 http://echonest.com 查看所有可用选项

轨迹

分析或获取有关曲目信息的方法。 包裹 EchoNest Track API

$trackApi = $echonest->getTrackApi();

请查看此项目中的文档或 http://echonest.com 查看所有可用选项

响应

API尝试返回您通常需要的信息,并为您提供状态代码,消息等信息,如果 回应成功。以下是完全呈现的响应的示例。

  Array
  (
      [status] => Array
          (
              [version] => 4.2
              [code] => 0
              [message] => Success
          )

  [artists] => Array
      (
          [0] => Array
              (
                  [name] => Radiohead
                  [id] => ARH6W4X1187B99274F
              )

      )

)

通常情况下,不需要状态信息。但是,如果您希望API返回完整的响应(通常是这样) 在处理寻呼机时需要,例如总计和起始参数被传递到艺术家数组之外), 将原始选项设置为您的API的 true

// pass options to getter
$response = $echonest->getArtistApi(array('raw' => true))->search(array('name' => 'Radiohead'));

// set options manually $artistApi = $echonest->getArtistApi(); $artistApi->setOption('raw', true); $response = $artistApi->search(array('name' => 'Radiohead'));

如果你认为这是愚蠢的,让我知道,我会考虑使用默认的 raw

沙盒

设置沙箱API以

开始
$sandboxApi = $echonest->getSandboxApi()->setOAuthConfig(array(
  "consumer_key" => $consumer_key,
  "consumer_secret" => $consumer_secret
))->setSandbox($sandbox_key);

获取所有可用资源的数组

$assets = $sandboxApi->assets($start, $per_page = 100);

访问单个资产

$sandboxApi->access($id)

访问资产数组

$sandboxApi->access($array)

To Do

更好的文档和测试报道将会即将推出




相关问题推荐