承接 prhl2375/php-discogs-api 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

prhl2375/php-discogs-api

Composer 安装命令:

composer require prhl2375/php-discogs-api

包简介

The Discogs API makes it easy for developers to communicate with the Discogs platform

关键字:

README 文档

README

Version License

This library is a PHP 7.3 / PHP 8.x implementation of the Discogs API v2.0. The Discogs API is a REST-based interface. By using this library you don't have to worry about communicating with the API: all the hard work has already be done.

License

This library is released under the MIT license. See the complete license in the LICENSE file.

Symfony Bundle

For the integration of Discogs into Symfony 5 or Symfony 6, see calliostro/discogs-bundle.

Installation

Start by installing composer. Next do:

$ composer require prhl2375/php-discogs-api

Requirements

PHP 7.3 or PHP 8.x

Usage

Creating a new instance is as simple as:

<?php

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

User-Agent

Discogs requires that you supply a User-Agent. You can do this easily:

<?php

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

Throttling

Discogs doesn't like it when you hit their API at a too high connection rate. Use the ThrottleSubscriber to prevent getting errors or banned:

<?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]);

Authentication

Discogs API allow to access protected endpoints with either a simple Discogs Auth Flow or a more advanced (and more complex) Oauth Flow

Discogs Auth

As stated in the Discogs Authentication documentation:

In order to access protected endpoints, you’ll need to register for either a consumer key and secret or user token, depending on your situation:

  • To easily access your own user account information, use a User token.
  • To get access to an endpoint that requires authentication and build 3rd party apps, use a Consumer Key and Secret.

Use Authorization header:

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

Authenticate with personal access token (you can get it from https://www.discogs.com/settings/developers):

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

OAuth

There are a lot of endpoints which require OAuth. Lucky for you using Guzzle this is peanuts. If you're having trouble obtaining the token and token_secret, please check out ricbra/php-discogs-api-example.

<?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'
]);

History

Another cool plugin is the History plugin:

<?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
}

More info and plugins

For more information about Guzzle and its plugins checkout the docs.

Perform a search:

Per august 2014 a signed OAuth request is required for this endpoint.

<?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());

Get information about a label:

<?php

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

Get information about an artist:

<?php

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

Get information about a release:

<?php

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

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

Get information about a master release:

<?php

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

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

Get image

Discogs returns the full url to images so just use the internal client to get those:

$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();
}

User lists

Get user lists

<?php

$userLists = $client->getUserLists([
    'username' => 'example',
    'page' => 1, #default
    'per_page' => 500 #min 1, max 500, default 50
]);

Get user list items

<?php

$listItems = $client->getLists([
    'list_id' => 1
]);

Get user wantlist

<?php

$wantlist = $client->getWantlist([
    'username' => 'example',
    'page' => 1, #default
    'per_page' => 500 #min 1, max 500, default 50
]);

User Collection

Authorization is required when folder_id is not 0.

Get collection folders

<?php

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

Get collection folder

<?php

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

Get collection items by folder

<?php

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

Listings

Creating and manipulating listings requires you to be authenticated as the seller

Create a Listing

<?php

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

Change Listing

<?php

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

Delete a Listing

<?php

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

Create Listings in bulk (via 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

Delete Listings in bulk (via CSV)

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

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

Documentation

Further documentation can be found at the Discogs API v2.0 Documentation.

Contributing

Implemented a missing feature? You can request it. And creating a pull request is an even better way to get things done.

Thanks to

Initial development by ricbra/php-discogs-api.

Porting to PHP 7 and adding more Discos API methods by AnssiAhola/php-discogs-api.

This API is build upon the shoulders of Guzzle 7.

prhl2375/php-discogs-api 适用场景与选型建议

prhl2375/php-discogs-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 189 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 89
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-25