PHP code example of w3spi5 / php-discogs-api

1. Go to this page and download the library: Download w3spi5/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/ */

    

w3spi5 / php-discogs-api example snippets




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



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



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

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

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

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



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



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



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




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




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




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

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



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

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


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




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



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



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



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



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



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



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



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



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


$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


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

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