calliostro/php-discogs-api
Lightweight Discogs API client for PHP 8.1+ with clean, modern APIs and Guzzle support. Access public artist/release/label data without registration, and enable search plus user features with consumer credentials. Includes optional Symfony Bundle integration.
Installation
composer require calliostro/php-discogs-api
Requires PHP 8.1+ (Guzzle 7+ and package dependencies).
Basic Setup Create a client instance using your Discogs OAuth 1.0a credentials (obtain from Discogs Developer Settings):
use Calliostro\Discogs\DiscogsClient;
$client = new DiscogsClient(
key: env('DISCOGS_KEY'),
secret: env('DISCOGS_SECRET'),
token: env('DISCOGS_TOKEN'),
tokenSecret: env('DISCOGS_TOKEN_SECRET')
);
First Use Case: Fetch an Artist
$artist = $client->getArtist(12345); // Replace with an actual artist ID
dd($artist->name, $artist->realname);
Explore the API
Check the Discogs API Documentation and the package’s README for available endpoints (e.g., listArtistReleases(), searchReleases()).
Searching for Releases
$results = $client->searchReleases(
query: 'Pink Floyd',
type: 'master',
perPage: 25
);
foreach ($results as $release) {
echo $release->title . ' (' . $release->year . ')' . PHP_EOL;
}
Batching Requests
Use Laravel’s HttpClient or Guzzle’s Promise for parallel requests:
$artistIds = [123, 456, 789];
$artists = collect($artistIds)->map(fn($id) => $client->getArtist($id));
Laravel Integration
Bind the client to the container in config/services.php:
'discogs' => [
'key' => env('DISCOGS_KEY'),
'secret' => env('DISCOGS_SECRET'),
'token' => env('DISCOGS_TOKEN'),
'token_secret' => env('DISCOGS_TOKEN_SECRET'),
],
Register in a service provider:
$this->app->singleton(DiscogsClient::class, fn() => new DiscogsClient(
config('services.discogs.key'),
config('services.discogs.secret'),
config('services.discogs.token'),
config('services.discogs.token_secret')
));
Inject via constructor:
public function __construct(private DiscogsClient $discogs) {}
Handling Pagination
Use paginate() or loop with offset/limit parameters:
$releases = $client->searchReleases(
query: 'Radiohead',
perPage: 50
);
while ($releases->hasMore()) {
$releases = $client->searchReleases(
query: 'Radiohead',
perPage: 50,
offset: $releases->offset + 50
);
}
Caching Responses Cache frequent queries using Laravel’s cache:
$release = Cache::remember("discogs_release_{$releaseId}", now()->addHours(1), fn() =>
$client->getRelease($releaseId)
);
OAuth 1.0a Requirements
key, secret, token, token_secret) from Discogs Developer Settings.Calliostro\Discogs\Exceptions\AuthenticationException.Method Naming Changes
getArtist() instead of artist()).Parameter Validation
searchReleases(query: string, type: string|null)).Rate Limiting
throttle middleware.Missing Endpoints
$response = $client->getGuzzleClient()->get('https://api.discogs.com/marketplace/...');
Enable Guzzle Debugging
Pass a HandlerStack with debug middleware:
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
$stack = HandlerStack::create();
$stack->push(Middleware::tap(function ($request) {
\Log::debug('Discogs Request:', ['url' => (string) $request->getUri()]);
}));
$client = new DiscogsClient(
key: env('DISCOGS_KEY'),
secret: env('DISCOGS_SECRET'),
token: env('DISCOGS_TOKEN'),
tokenSecret: env('DISCOGS_TOKEN_SECRET'),
guzzleClient: new Client(['handler' => $stack])
);
Inspect Raw Responses Access the underlying Guzzle response:
try {
$release = $client->getRelease(123);
} catch (\Exception $e) {
$rawResponse = $client->getGuzzleClient()->get('...')->getBody();
\Log::error($rawResponse);
}
Custom Resources
Extend the base Resource class to add methods:
class CustomRelease extends \Calliostro\Discogs\Resources\Release {
public function isVinyl(): bool {
return str_contains($this->format, 'Vinyl');
}
}
Override the client’s hydration logic if needed.
Add New Endpoints
Extend the DiscogsClient class:
public function getUserCollection(string $username): array {
$response = $this->getGuzzleClient()->get("/users/{$username}/collection");
return $this->hydrateCollection($response->getBody());
}
Mocking for Tests
Use Laravel’s HttpClient mocking or replace the client:
$mock = Mockery::mock(DiscogsClient::class);
$mock->shouldReceive('getRelease')->andReturn(new Release(['title' => 'Mock Release']));
Webhook Integration
For real-time updates, use Discogs’ webhook system and handle payloads via Laravel’s Broadcast or Queue systems.
Type-Safe Parameters Leverage PHP 8.1+ named arguments and type hints:
$client->searchReleases(
query: 'Artist Name',
type: 'master', // string|null
perPage: 25, // int
year: 2023 // int|null
);
How can I help you explore Laravel packages today?