corncodecreators/discogs-api-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

corncodecreators/discogs-api-client

Composer 安装命令:

composer require corncodecreators/discogs-api-client

包简介

Unofficial official client for Discogs API

README 文档

README

The official unofficial

Discogs-Api-Client for PHP

This API-Client was built on PHP to interact with Discogs API by supporting both authentication methods and providing a clean way to act with the data via DTO-Objects.

Remark:
This Client supports already the main required functions, improvements will come over time, and contribution is always welcome.

Quick'n Dirty Example

This client will provide you quickly access to the Discogs API and deliver nice DTO-objects holding the data, so that you can easily use it in your PHP-application.

// create your client object
$discogsApiClient = new PersonalTokenClient();

// load the desired release-data from the Discogs API
$release = $discogsApiClient->requestDatabase()->getRelease(123);

// get the title of the release
$title = $release->getTitle();

// show all images of the release
foreach ($release->getImages() as $image) {
    $url = $image->getResourceUrl();
    echo "<img alt='{$title}' src='{$url}'>";
}

Features

  • Easy: This client is super easy to use!
  • Interaction with Discogs API: Get all data you need from Discogs and interact with it!
  • Support for both Authentication Methods: Easily switch between OAuth 1.0a and Discogs Token.
    • Personal Access Tokens: For accessing public resources and private user data.
    • OAuth: For more complex scenarios requiring user authorization.
  • DTO-Based Design: The library is designed with a strong emphasis on Data Transfer Objects (DTOs), promoting clean and maintainable PHP code. The use of DTOs ensures well-structured and type-safe data handling and readable code.

Requirements

  • PHP 8.1 or higher
  • A valid Discogs account and API key/token

Setup

Installation

Use Composer to add the client to your project:

composer require corncodecreators/discogs-api-client

Configure your credentials (optional)

Put these variables into your .env-file, so you do not to need to put your credentials into the constructor of the API-Client.

DISCOGS_USER_AGENT=your_agent_name
DISCOGS_USER_TOKEN=your_token

or

DISCOGS_USER_AGENT=your_agent_name
DISCOGS_CONSUMER_KEY=your_consumer_key
DISCOGS_CONSUMER_SECRET=your_consumer_secret

Client-Initialization

There are two clients to interact with the API. Either you use your PersonTokenClient, or the OAuth-client.

Personal-Token-Client

This is the simplest way to interact with Discogs API and is supporting access to everything you can get from Discogs. You should start with this solution and only swap to the OAuth-Client, if you really see a need.

Using Env-Variables

use CornCodeCreators\Discogs\Client\PersonalTokenClient;

$discogsApiClient = new PersonalTokenClient();
$release = $discogsApiClient->requestDatabase()->getRelease(1234);

// ...

Using Parameters

Remark: You should try to avoid this solution to avoid compromising your credentials.

use CornCodeCreators\Discogs\Client\PersonalTokenClient;

$userAgent = 'MyAgent';
$userToken = 'abcdefg';

$discogsApiClient = new PersonalTokenClient($userAgent, $userToken);
$release = $discogsApiClient->requestDatabase()->getRelease(1234);

// ...

OAuth-Client

Using Env-Variables

use CornCodeCreators\Discogs\Client\OAuthClient;

$discogsApiClient = new OAuthClient();
$release = $discogsApiClient->requestDatabase()->getRelease(1234);

// ...
Using Parameters

Remark: You should try to avoid this solution to avoid compromising your credentials.

use CornCodeCreators\Discogs\Client\OAuthClient;

$userAgent      = 'MyAgent';
$consumerKey    = 'abcdefg';
$consumerSecret = '1234567';

$discogsApiClient = new OAuthClient($userAgent, $consumerKey, $consumerSecret);
$release = $discogsApiClient->requestDatabase()->getRelease(1234);
Symfony example

The OAuth-Flow requires multiple interaction steps between your application via this client and the Discogs-Server. This requires typically multiple controllers on your side. The example below is using Symfony

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use CornCodeCreators\Discogs\Client\OAuthClient;

class OAuthController extends AbstractController
{
    private $discogsApiClient;

    public function __construct(
    ) {
        $this->discogsApiClient = new OAuthClient();
    }

    #[Route('/oauth', name: 'app_oauth_step1')]
    public function oauth(): RedirectResponse
    {
        $callbackUrl = $this->generateUrl('discogs_callback', [], UrlGeneratorInterface::ABSOLUTE_URL);
        $tokens      = $this->discogsApiClient->getRequestToken($callbackUrl);

        return $this->redirect($this->discogsApiClient->getAuthorizeUrl());
    }

    #[Route('/callback', name: 'discogs_callback')]
    public function callback(Request $request): RedirectResponse
    {
        $oauthVerifier = $request->query->get('oauth_verifier');
        $tokens        = $this->discogsApiClient->getAccessToken($oauthVerifier);

        return $this->redirectToRoute('app_homepage');
    }

    #[Route('/home', name: 'app_homepage')]
    public function home(): void
    {
        dump("Hello world! -> 'Dummy Homepage");

        $release = $this->discogsApiClient->requestDatabase()->getRelease(249504);

        dd($release);
    }
}

Making Requests

The requests follow the structure provided by the API-Documentation

  1. Database
  2. UserIdentity
  3. (more to come)

Remark: This is not an exhausting list, but only good examples to start!

Database

$data = $discogsApiClient->requestDatabase()->getRelease(249504);
$data = $discogsApiClient->requestDatabase()->getReleaseRatingOfUser(399193, 'Someone');
$data = $discogsApiClient->requestDatabase()->getReleaseRatingOfCommunity(399193);
$data = $discogsApiClient->requestDatabase()->getReleaseStats(399193);
$data = $discogsApiClient->requestDatabase()->getMasterRelease(1000);
$data = $discogsApiClient->requestDatabase()->getMasterReleaseVersions(1000);
$data = $discogsApiClient->requestDatabase()->getArtist(108713);
$data = $discogsApiClient->requestDatabase()->getLabel(1);
$data = $discogsApiClient->requestDatabase()->getArtistReleases(108713, ['per_page' => 42]);
$data = $discogsApiClient->requestDatabase()->getLabelReleases(1, ['per_page' => 5]);
$data = $discogsApiClient->requestDatabase()->searchAllTypes(['q' => 'Nirvana', 'per_page' => 5]);
$data = $discogsApiClient->requestDatabase()->addReleaseRatingOfUser(400788, 'Someone', 3);
$data = $discogsApiClient->requestDatabase()->removeReleaseRatingOfUser(400788, 'Someone');

UserIdentity

$data = $discogsApiClient->requestUser()->getIdentity();
$data = $discogsApiClient->requestUser()->getProfile("Someone");
$data = $discogsApiClient->requestUser()->getSubmissions("Someone");
$data = $discogsApiClient->requestUser()->getContributions("Someone");
$data = $discogsApiClient->requestUser()->updateProfile("Someone", ['name' => 'Jon Doe', 'profile' => 'Someone nobody knows']);

Documentation

Refer to the Discogs Developer Documentation for detailed information on API endpoints and authentication methods.

Contributing

Feel free to fork this repository and contribute by submitting a pull request. Any contributions to enhance functionality or add features are welcome!

License

This project is licensed under the MIT License. See the MIT License. file for more details.

corncodecreators/discogs-api-client 适用场景与选型建议

corncodecreators/discogs-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「api」 「client」 「discogs」 「dto」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 corncodecreators/discogs-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 corncodecreators/discogs-api-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 corncodecreators/discogs-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-31