【问题标题】:PHP MusicBrainz get the first release datePHP MusicBrainz 获得第一个发布日期
【发布时间】:2016-02-18 15:50:09
【问题描述】:

我正在尝试使用 Musicbrainz 获取歌曲的第一个发布日期。为此,我使用了 mikealmond musicBrainz 库。

我遇到的问题是,当我尝试执行与此示例完全相同的代码 (https://github.com/mikealmond/MusicBrainz/blob/master/examples/first-recording-search.php) 时,我总是遇到身份验证错误。

Client error response [status code] 401 [reason phrase] Unauthorized [url] http://musicbrainz.org/ws/2/artist/0383dadf-2a4e-4d10-a46a-e9e041da8eb3?inc=releases+recordings+release-groups+user-ratings&fmt=json

因此我尝试将我的用户名和密码添加到请求中:

$brainz = new MusicBrainz(new GuzzleHttpAdapter(new Client()),'myusername','mypassword'); 
$brainz->setUserAgent('myapplicationname', '0.2', 'http://localhost:443/');

如果我手动调用错误消息中的 url 并输入我的用户名和密码,我会得到我期望的数组。

我刚刚发现:如果我删除了 -"+ user - ratings"- 它不需要身份验证。

因此我在我的项目中用"user - ratings" 注释了这些行

现在我认为它可以工作,但是查询的性能非常糟糕,并且经常出现错误 503 // MusicBrainz Web 服务器当前正忙。请稍后再试。 // 只唱一首歌就需要几秒钟。有人知道这是正常的还是我还有什么错误?

我的代码....

//Create new MusicBrainz object
    $brainz = new MusicBrainz(new GuzzleHttpAdapter(new Client()), 'username', 'password');
    $brainz->setUserAgent('applicationname', '0.2', 'http://localhost:443/');

    // set defaults
    $artistId = null;
    $songId = null;
    $lastScore = null;
    $firstRecording = array(
        'releaseDate' => new DateTime()
    );
    // Set the search arguments to pass into the RecordingFilter
    $args = array(
        "recording" => 'we will rock you',
        "artist" => 'Queen',
    );
    try {
        // Find all the recordings that match the search and loop through them
        $recordings = $brainz->search(new RecordingFilter($args));

$recorings 我可以打印并且在循环中我可以打印每个 $recording,但是当我提取信息时出现错误


 /** @var $recording \MusicBrainz\Recording */
            foreach ($recordings as $recording) {
                // if the recording has a lower score than the previous recording, stop the loop.
                // This is because scores less than 100 usually don't match the search well
                if (null != $lastScore && $recording->getScore() < $lastScore) {
                    break;
                }
                $lastScore = $recording->getScore();
                $releaseDates = $recording->getReleaseDates();
                $oldestReleaseKey = key($releaseDates);
                if (strtoupper($recording->getArtist()->getName()) == strtoupper($args['artist'])
                    && $releaseDates[$oldestReleaseKey] < $firstRecording['releaseDate']
                ) {
                    $firstRecording = array(
                        'releaseDate' => $recording->releases[$oldestReleaseKey]->getReleaseDate()
                    );
                }
            }
            pr($firstRecording);
        } catch (Exception $e) {
            pr($e->getMessage());
        }

【问题讨论】:

  • 您的用户名或密码中有non-ascii 字符吗?
  • @JonnyJD no :/ 用户名和密码仅由字母组成...请看我更新的问题!没有“用户评级”,我得到了版本,它不需要身份验证......但性能非常糟糕,而且我经常收到错误响应 502 -> MusicBrainz 网络服务器当前很忙。请稍后再试。我的代码显示了问题出在哪里......
  • 不幸的是,这些天 503 错误是正常的,与您的代码无关:-/ 服务器人员正在处理它。你应该做的是测试代码中的错误(如果库没有这样做),然后在几秒钟后重试(或类似的)。

标签: php musicbrainz


【解决方案1】:
 $brainz = new MusicBrainz(new GuzzleHttpAdapter(new Client()), 'username', 'password');

您必须设置您的 MusicBrainz 帐户凭据。将“username”替换为您的帐户用户名,将“password”替换为用于登录 MusicBrainz.org 的密码

【讨论】:

    猜你喜欢
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多