0

I'm making a website where I'd like the user to be able to start typing in a band name (for example, "Rad") and have Discogs API display 10 most similar suggestions to them (for example, "Radical Face", "Radiohead", etc). These suggestions could be sorted either alphabetically or, ideally, by popularity.

The problem is that I don't know how to make such a request to the Discogs API. Here's the code I'm working with now, which retrieves the content of http://api.discogs.com/releases/1 and parses it.

Any insight would be appreciated. Thank you.

    <?php
        $url = "http://api.discogs.com/releases/1"; // add the resource info to the url. Ex. releases/1

        //initialize the session
        $ch = curl_init();

        //Set the User-Agent Identifier
        curl_setopt($ch, CURLOPT_USERAGENT, 'SiteName/0.1 +http://your-site-here.com');

        //Set the URL of the page or file to download.
        curl_setopt($ch, CURLOPT_URL, $url);

        //Ask cURL to return the contents in a variable instead of simply echoing them
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //Execute the curl session
        $output = curl_exec($ch);

        //close the session
        curl_close ($ch);



        function textParser($text, $css_block_name){

            $end_pattern = '], "';

            switch($css_block_name){
                # Add your pattern here to grab any specific block of text
                case 'description';
                    $end_pattern = '", "';
                    break;
            }

            # Name of the block to find
            $needle = "\"{$css_block_name}\":";

            # Find start position to grab text
            $start_position = stripos($text, $needle) + strlen($needle);

            $text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
            $text_portion = str_ireplace("[", "", $text_portion);
            $text_portion = str_ireplace("]", "", $text_portion);

            return $text_portion;
        }

        $blockStyle = textParser($output, 'styles');
        echo $blockStyle. '<br/>';

        $blockDescription = textParser($output, 'description');
        echo $blockDescription. '<br/>';



    ?> 
m0a
  • 1,005
  • 2
  • 15
  • 29
  • Did you check their documentation? http://www.discogs.com/developers/ – h7r Jan 02 '16 at 23:16
  • I did, but it's not... in-depth enough for me. I asked for more specific assistance here ( http://www.discogs.com/forum/thread/702084 ) ... It's been 2 days of confusion. – m0a Jan 02 '16 at 23:53

1 Answers1

0

With the discogs API you can easily execute a search. I think you have already viewed the documentation: https://www.discogs.com/developers/#page:database,header:database-search

There you can even say that you only want to search for artists. When you retrieve the results you must either sort them alphabetically by yourself or must relay on the order of the results. I think that order is already some kind of popularity by discogs as far as I can see from the documentation. And it is the same implementation as in the website integrated search.

You should keep in mind that the result set can be very large. So sorting by alphabet wouldn't be the best idea as you have to retrieve all result pages. Here you should increase the page_size parameter to the maximum of 100 items per page.

jmizv
  • 1,172
  • 2
  • 11
  • 28