tafoyaventures/php-discogs-api

Discogs API 使开发者能够轻松与 Discogs 平台进行通信

v2.0.0 2022-07-12 14:49 UTC

README

Build Status Latest Stable Version Total Downloads License Quality

此库是 Discogs API v2.0 的 PHP 7.4 实现。[Discogs API](http://www.discogs.com/developers/index.html) 是一个基于 REST 的接口。通过使用此库,您无需担心与 API 的通信:所有艰难的工作都已经完成。

此 API 建立在巨人之上:[Guzzle 7.0](http://guzzle.readthedocs.org/en/latest/)。这是一个绝对出色的库。

许可证

此库以 MIT 许可证发布。请参阅 LICENSE 文件中的完整许可证。

安装

首先通过 安装 composer。接下来做

$ composer require tafoyaventures/php-discogs-api

要求

PHP >=7.4.0

使用

创建一个新的实例非常简单

<?php

$client = Discogs\ClientFactory::factory([]);

用户代理

Discogs 要求您提供一个 User-Agent。您可以通过这种方式轻松实现

<?php

$client = Discogs\ClientFactory::factory([    
    'headers' => ['User-Agent' => 'your-app-name/0.1 +https://www.awesomesite.com'],
]);

节流

Discogs 不喜欢您以过高的连接速率调用他们的 API。使用 ThrottleSubscriber 防止出现错误或被禁止

<?php

$handler = \GuzzleHttp\HandlerStack::create();
$throttle = new Discogs\Subscriber\ThrottleSubscriber();
$handler->push(\GuzzleHttp\Middleware::retry($throttle->decider(), $throttle->delay()));

$client = Discogs\ClientFactory::factory(['handler'=>$handler]);

身份验证

Discogs API 允许您使用简单的 [Discogs Auth Flow](https://www.discogs.com/developers/#page:authentication,header:authentication-discogs-auth-flow) 或更高级(更复杂)的 [OAuth Flow](https://www.discogs.com/developers/#page:authentication,header:authentication-oauth-flow) 访问受保护端点

Discogs 身份验证

如 Discogs 身份验证文档所述

为了访问受保护端点,您需要根据情况注册消费者密钥和密钥或用户令牌

  • 要轻松访问您自己的用户账户信息,请使用 用户令牌
  • 要访问需要身份验证的端点并构建第三方应用程序,请使用 消费者密钥和密钥

使用 Discogs Php API,您可以通过向自己的默认设置中添加一个 query 键来添加您的凭据,如下所示

使用 query 替换 Authorization 标头

$client = ClientFactory::factory([
    'headers' => [
        'Authorization' => "Discogs key=key_here, secret=secret_here",
    ],
]);

使用个人访问令牌进行身份验证,您可以从 https://www.discogs.com/settings/developers 获取

$client = ClientFactory::factory([
    'headers' => [
        'User-Agent' => $user_agent,
        'Authorization' => "Discogs token={$access_token}",
    ]
]);

OAuth

有许多端点需要 OAuth。幸运的是,使用 Guzzle 这很容易。如果您在获取令牌和令牌密钥方面遇到困难,请查看我的 示例实现

<?php

$oauth = new GuzzleHttp\Subscriber\Oauth\Oauth1([
    'consumer_key'    => $consumerKey, // from Discogs developer page
    'consumer_secret' => $consumerSecret, // from Discogs developer page
    'token'           => $token['oauth_token'], // get this using a OAuth library
    'token_secret'    => $token['oauth_token_secret'] // get this using a OAuth library
]);
$handler = GuzzleHttp\HandlerStack::create();
$handler->push($oauth);
$client = Discogs\ClientFactory::factory([
    'handler' => $handler,
    'auth' => 'oauth'
]);

历史

另一个很酷的插件是历史插件

<?php

$container = [];
$history = GuzzleHttp\Middleware::History($container);
$handler = GuzzleHttp\HandlerStack::create();
$handler->push($history);
$client = Discogs\ClientFactory::factory([ 
    'handler' => $handler
]);

$response = $client->search([
    'q' => 'searchstring'
]);

foreach ($container as $row) {
    print $row['request'] -> getMethod(); // GET
    print $row['request'] -> getRequestTarget(); // /database/search?q=searchstring
    print strval($row['request'] -> getUri()); // https://api.discogs.com/database/search?q=searchstring
    print $row['response'] -> getStatusCode(); // 200
    print $row['response'] -> getReasonPhrase(); // OK
}

更多信息插件

有关 Guzzle 及其插件的更多信息,请参阅 文档

执行搜索

截至 2014 年 8 月,此端点需要签名 OAuth 请求。

<?php

$response = $client->search([
    'q' => 'Meagashira'
]);
// Loop through results
foreach ($response['results'] as $result) {
    var_dump($result['title']);
}
// Pagination data
var_dump($response['pagination']);

// Dump all data
var_dump($response->toArray());

获取标签信息

<?php

$label = $service->getLabel([
    'id' => 1
]);

获取艺术家信息

<?php

$artist = $service->getArtist([
    'id' => 1
]);

获取发行信息

<?php

$release = $service->getRelease([
    'id' => 1
]);

echo $release['title']."\n";

获取母盘发行信息

<?php

$master  = $service->getMaster([
    'id' => 1
]);

echo $master['title']."\n";

获取图片

Discogs 返回图片的完整 URL,因此只需使用内部客户端即可获取这些图片

$release = $client->getRelease([
    'id' => 1
]);
foreach ($release['images'] as $image) {
    $response = $client->getHttpClient()->get($image['uri']);
    // response code
    echo $response->getStatusCode();
    // image blob itself
    echo $client->getHttpClient()->get($image['uri'])->getBody()->getContents();
}

用户收藏

folder_id 不为 0 时,需要授权。

获取收藏夹

<?php

$folders = $service->getCollectionFolders([
    'username' => 'example'
]);

获取收藏夹

<?php

$folder = $service->getCollectionFolder([
    'username' => 'example',
    'folder_id' => 1
]);

按文件夹获取收藏夹项目

<?php

$items = $service->getCollectionItemsByFolder([
    'username' => 'example',
    'folder_id' => 3
]);

列表

创建和操作列表需要您作为卖家进行身份验证

创建列表

<?php

$response = $client->createListing([
    'release_id' => '1',
    'condition' => 'Good (G)',
    'price' => 3.49,
    'status' => 'For Sale'
]);

更改列表

<?php

$response = $client->changeListing([
    'listing_id' => '123',
    'condition' => 'Good (G)',
    'price' => 3.49,
]);

删除列表

<?php

$response = $client->deleteListing(['listing_id' => '123']);

批量创建列表(通过 CSV)

<?php
$response = $client->addInventory(['upload' => fopen('path/to/file.csv', 'r')]);

// CSV format (example): 
// release_id,condition,price
// 1,Mint (M),19.99
// 2,Near Mint (NM or M-),14.99

批量删除列表(通过 CSV)

<?php
$response = $client->deleteInventory(['upload' => fopen('path/to/file.csv', 'r')]);

// CSV format (example): 
// listing_id
// 123
// 213
// 321

文档

更多文档可以在Discogs API v2.0 文档中找到。

贡献

实现了缺失的调用吗?欢迎提交PR!

Bitdeli Badge