PHP code example of ampache / php-discogs-api

1. Go to this page and download the library: Download ampache/php-discogs-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ampache / php-discogs-api example snippets


$page  = $discogs->get_label_releases(1);
$total = $page['pagination']['pages'];

for ($number = 1; $number <= $total; $number++) {
    $releases = $discogs->get_label_releases(1, $number)['releases'];
    // ...
}

$byId = [];
for ($number = 1; $number <= $total; $number++) {
    foreach ($discogs->get_artist_releases($artistId, $number)['releases'] as $release) {
        $byId[$release['id']] = $release;
    }
}

use AmpacheDiscogs\DiscogsException;

try {
    $album = $discogs->get_master(1234);
} catch (DiscogsException $error) {
    if ($error->getStatusCode() === 404) {
        // no such record, nothing to retry
    } elseif ($error->isRateLimited()) {
        // still limited after three attempts, come back later
    }

    print_r($error->getMessage());
}



use AmpacheDiscogs\Discogs;

> 'The Shape',
        'albumartist' => 'Code 64',
    ],
];

echo "Checking: " . print_r($media, true) . PHP_EOL;
try {
    // your own Discogs api key and secret are ist=Code+64&per_page=10&key=key@secret=secret
     */
    $albums = $discogs->search_album($media['albumartist'], $media['album']);
    if (empty($albums['results'])) {
        $albums = $discogs->search_album($media['albumartist'], $media['album'], 'release');
    }

    // get the album that matches $artist - $album
    if (!empty($albums['results'])) {
        foreach ($albums['results'] as $albumSearch) {
            if ($media['albumartist'] . ' - ' . $media['album'] === $albumSearch['title']) {
                /**
                 * @var array{
                 *     country: string,
                 *     year: string,
                 *     format: string[],
                 *     label: string[],
                 *     type: string,
                 *     genre: string[],
                 *     style: string[],
                 *     id: ?int,
                 *     barcode: string[],
                 *     master_id: int,
                 *     master_url: string,
                 *     uri: string,
                 *     catno: string,
                 *     title: string,
                 *     thumb: string,
                 *     cover_image: string,
                 *     resource_url: string,
                 *     community: object,
                 *     format_quantity: ?int,
                 *     formats: ?object,
                 * } $albumSearch
                 */
                $album = $albumSearch;
                break;
            }
        }

        // look up the master release if we have one or the first release
        if (!isset($album['id'])) {
            /**
             * @var array{
             *     id: ?int,
             *     main_release: int,
             *     most_recent_release: int,
             *     uri: string,
             *     versions_uri: string,
             *     main_release_uri: string,
             *     most_recent_release_uri: string,
             *     num_for_sale: int,
             *     lowest_price: int,
             *     images: object,
             *     genres: string[],
             *     styles: string[],
             *     year: int,
             *     tracklist: object,
             *     artists: object,
             *     title: string,
             *     data-quality: string,
             *     videos: object,
             * } $album
             */
            $album = (($albums['results'][0]['master_id'] ?? 0) > 0)
                ? $discogs->get_album((int)$albums['results'][0]['master_id'])
                : $discogs->get_album((int)$albums['results'][0]['id'], 'releases');
        }

        // fallback to the initial search if we don't have a master
        if (!isset($album['id'])) {
            $album = $albums['results'][0];
        }

        print_r($album);
    }

} catch (Exception $exception) {
    print_r($exception->getMessage());
}