如何在Disqus中获取评论数量?

10
我希望将 Disqus 评论计数存储在我的数据库中,以便我可以根据评论计数对文章进行排序。基本上,每当某个页面在我的网站上被阅读时,我想问 Disqus 这个页面有多少评论,然后更新数据库中的计数。 http://docs.disqus.com/help/3/ 看起来并没有什么帮助。请问有什么建议吗?

您提供的链接已经不存在了。新页面是在主页上添加评论计数链接,看起来非常有帮助。 - Marco Panichi
4个回答

8

使用disqus API获取评论计数

在开始之前,您需要完成以下操作:

注册Disqus API密钥 (可选)拥有自己的网站以替换示例数据

注意:您使用的URL必须与Disqus中设置的URL匹配。请参阅Web Integration文档,了解如何可靠地设置此项。

示例HTML

<!DOCTYPE html>
<html>
    <head>
        <title>Disqus Comment Counts Example</title>
    </head>
    <body>
        <h1>Comment Counts Example</h1>
        <div>
            <a href="http://thenextweb.com/google/2013/05/03/fullscreen-beam-launches-first-youtube-app-for-google-glass-with-public-or-private-sharing/">
                <h2>Fullscreen BEAM: The first YouTube app for Google Glass comes with public or private sharing</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/google/2013/05/03/fullscreen-beam-launches-first-youtube-app-for-google-glass-with-public-or-private-sharing/"></div>
            </a>
        </div>
        <div>
            <a href="http://thenextweb.com/apps/2013/05/04/traktor-dj/">
                <h2>Traktor DJ: Native Instruments remixes its impressive DJ software for iPhone</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/apps/2013/05/04/traktor-dj/"></div>
            </a>
        </div>
        <div>
            <a href="http://thenextweb.com/video/2013/05/04/ninja-innovation-in-the-21st-century-with-gary-shapiro-of-the-consumer-electronics-association-at-tnw2013-video/">
                <h2>Ninja innovation in the 21st Century with the Consumer Electronics Association&#8217;s Gary Shapiro [Video]</h2>
                <div class="count-comments" data-disqus-url="http://thenextweb.com/video/2013/05/04/ninja-innovation-in-the-21st-century-with-gary-shapiro-of-the-consumer-electronics-association-at-tnw2013-video/"></div>
            </a>
        </div>
        <button type="button" id="get-counts-button">Get Comment Counts</button>
    </body>
</html>

变量:

<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
  var disqusPublicKey = "YOUR_PUBLIC_KEY";
  var disqusShortname = "thenextweb"; // Replace with your own shortname

  var urlArray = [];
  $('.count-comments').each(function () {
    var url = $(this).attr('data-disqus-url');
    urlArray.push('thread:link='+url);
  });
});
</script>

制作请求 API
$('#get-counts-button').click(function () {

    $.ajax({
        type: 'GET',
        url: 'https://disqus.com/api/3.0/threads/set.json?'+urlArray.join('&')+'&forum='+disqusShortname+'&api_key='+disqusPublicKey,
        cache: false,
        dataType: 'json',
        success: function (result) {

          for (var i in result.response) {

            var countText = " comments";
            var count = result.response[i].posts;

            if (count == 1) {
              countText = " comment";
            }

            $('[data-disqus-url="' + result.response[i].link + '"]').html('<h4>' + count + countText + '</h4>');
          }
        }
    });
});

你能解释一下这个URL的结构吗:https://disqus.com/api/3.0/threads/set.jsonp?那么,当我们想要获取计数时,我们需要设置像这样的URL:https://disqus.com/api/3.0/threads/set.jsonp。 - HMdeveloper
谢谢您的回答,但是我只得到了5篇文章的计数响应。是否有任何限制或者我应该分批发送请求以获取5篇文章?您有什么建议? - Dinesh Patil
可能是Disqus API发生了一些变化,因为我现在需要发送多个thread:link参数,而不是一个包含字符串数组的线程参数,格式为“link:https://example.com”。所以我将 'thread:link='+url 推入数组中,然后使用 & 连接该数组。 - fvrab

3

10
由于评论计数是公共信息,可惜的是似乎获取它变得不必要地复杂。 - filip

1

1

我知道这是一个老问题,但是谷歌搜索出了很多这样的SO问题(这是排名第一的结果),大多数没有实质性的答案或者依靠Github API的答案似乎不太可靠。


我已经苦苦寻找了几天如何获取评论数,尝试了那个API类,但似乎会导致我的应用程序崩溃出现致命错误。

在进一步搜索后,我找到了Disqus API的JSON输出链接,并通过一些尝试编写了一个快速获取评论计数的函数:

function getDisqusCount($shortname, $articleUrl) {
        $json = json_decode(file_get_contents("https://disqus.com/api/3.0/forums/listThreads.json?forum=".$shortname."&api_key=".$YourPublicAPIKey),true);

        $array = $json['response'];
        $key = array_search($articleUrl, array_column($array, 'link'));
        return $array[$key]['posts'];
    }

您需要注册一个应用程序来获取公共API密钥,可以在此处完成:https://disqus.com/api/applications/

然后,此函数将仅输出评论的总数,您可以将其存储在数据库或其他地方。

此函数的作用:

$json数组返回有关您的评论插件所在页面的许多信息。例如:

Array
(
[0] => Array
(
  [feed] => https://SHORTNAME.disqus.com/some_article_url/latest.rss
    [identifiers] => Array
    (
      [0] => CUSTOMIDENTIFIERS
    )

[dislikes] => 0
[likes] => 0
[message] => 
[id] => 5571232032
[createdAt] => 2017-02-21T11:14:33
[category] => 3080471
[author] => 76734285
[userScore] => 0
[isSpam] => 
[signedLink] => https://disq.us/?url=URLENCODEDLINK&key=VWVWeslTZs1K5Gq_BDgctg
[isDeleted] => 
[raw_message] => 
[isClosed] => 
[link] => YOURSITEURLWHERECOMMENTSARE
[slug] => YOURSITESLUG
[forum] => SHORTNAME
[clean_title] => PAGETITLE
[posts] => 0
[userSubscription] => 
[title] => BROWSERTITLE
[highlightedPost] => 
)

 [1] => Array
 (
   ... MORE ARRAYS OF DATA FROM YOUR SHORTNAME FORUM ... etc
 )
)

由于数组返回时没有任何有用的顶层数组键,因此我们通过我们已知的列名键在数组上执行array_search操作:您的页面URL,其中包含评论插件([link])。
然后将返回顶层数组键,在本例中为0,然后我们可以从数组中提取我们想要的信息,例如总评论数(数组键posts)。
希望这能帮助某些人!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接