简体   繁体   English

如何使用Discogs API显示名称相似的艺术家?

[英]How to use Discogs API to display similarly named artists?

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). 我正在创建一个网站,希望用户能够开始输入乐队名称(例如“ Rad”),并让Discogs API显示与他们最相似的10条建议(例如“ Radical Face” ,“ Radiohead”等)。 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. 问题是我不知道如何向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. 这是我现在正在使用的代码,该代码检索http://api.discogs.com/releases/1的内容并进行解析。

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/>';



    ?> 

With the discogs API you can easily execute a search. 使用discogs API,您可以轻松执行搜索。 I think you have already viewed the documentation: https://www.discogs.com/developers/#page:database,header:database-search 我认为您已经阅读了文档: 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. 我认为,从文档中可以看到,该订单已经受到discog的欢迎。 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. 在这里,您应该将page_size参数增加到每页最多100个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM