• theyearofi over 8 years ago

    Hi all, I wrote a set of classes for communicating with the Discogs API using the Zend Framework for personal use, but I have put it up on git hub for anyone who is interested.

    http://github.com/ryanhorn/AOneFiveThree/

    Technically, the only class that is tied to the Zend Framework is AOneFiveThree/Service/Discogs.php (as it uses Zend_Rest_Client and Zend_Service_Exception), so it shouldn't be too hard to adapt it to another framework/library.

    Usage is simple. Place AOneFiveThree/Service in your include path. Then instantiate the service class (passing in your api key) and use the "getRelease", "getArtist", "getLabel" and "search" methods. Each method takes the query to run as its first parameter and an optional array of options as its second parameter. The return values are objects providing an interface to the returned data.

    Ex:

    require_once('AOneFiveThree/Service/Discogs.php');

    $discogsService = new AOneFiveThree_Service_Discogs('myapikey');

    // Get release 2253877
    $release = $discogsService->getRelease(2253877);

    // $release->artists[0] === 'Flying Lotus'
    // $release->extraArtists[0]->name === 'Miguel Atwood-Ferguson'
    // $release->formats[0]->descriptions[0] === 'LP'
    // etc.

    // Get artist "Autechre"
    $artist = $discogsService->getArtist('Autechre');

    // $artist->aliases[0] === 'Lego Feet'
    // $artist->images[0]->uri === 'http://www.discogs.com/image/A-41-001.jpg'
    // $artist->realName === 'Rob Brown, Sean Booth'
    // etc.

    // Get label "Brainfeeder"
    $label = $discogsService->getLabel('Brainfeeder');

    // $label->profile === 'Record label run by Flying Lotus.'
    // $label->releases[0]->artist === 'Daedelus'
    // $label->urls[0] === 'http://www.brainfeedersite.com/'
    // etc.

    // Get the 2nd page of search results for "Hecker"
    $searchResults = $discogsService->search("Hecker", array('page' => 2));

    // $searchResults->end === 40
    // $searchResults->numberOfResults === 307
    // $searchResults->results[0]->uri === 'http://www.discogs.com/Hecker-IT-ISO161975/release/11271'
    // etc.

    // The service object stores the number of requests made in the last 24 hours after each request in $discogsService->numberOfRequestsMadeInLast24Hours;

    Let me know if you have any questions/suggestions/difficulties.
  • theyearofi over 8 years ago

    theyearofi edited over 8 years ago
    The data objects can also be used in part to parse the monthly data dumps as well. Releases example:

    Download the releases xml dump.
    Since I'm using XMLReader, you'll need to wrap all release elements in a root tag (they currently do not come this way). On *nix, via command line you can do this by using the following command (replacing the ()'s around root and /root with angle brackets):
    echo "(root)"|cat - DUMPFILE.xml > /tmp/out && mv /tmp/out DUMPFILE.xml && echo "(/root)" >> DUMPFILE.xml

    Then the following PHP will be able to parse the file (or http://pastebin.com/W1F5Cw4W for a more readable version)

    $pathToXml = '/path/to/DUMPFILE.xml';

    $reader = new XMLReader();
    $reader->open($pathToXml);

    while ($reader->read()) {

    if ($reader->name === 'release' && $reader->nodeType === XMLReader::ELEMENT) {
    $xml = $reader->readOuterXML();
    $dom = new DOMDocument();
    $dom->loadXML($xml);
    require_once('AOneFiveThree/Service/discogs/Release.php');
    $release = new AOneFiveThree_Service_Discogs_Release($dom->documentElement);

    /*
    * Do what you want with the data here
    */

    unset($release);
    unset($dom);
    }
    }

    $reader->close();

Log In You must be logged in to post.