如何在Disqus中获得评论数?

时间:2012-02-05 14:22:43

标签: php javascript disqus

我想将Disqus评论计数存储在我自己的数据库中,以便我可以按评论计数对文章进行排序。基本上,每次在我的网站上阅读页面时,我都想问一下这个页面有多少评论,然后用这个数字更新数据库。

http://docs.disqus.com/help/3/似乎没有帮助。

有什么建议吗?

4 个答案:

答案 0 :(得分:6)

使用disqus API获取评论计数

以下是您在开始之前需要做的事情:

注册Disqus API key (可选)拥有自己的站点来替换示例数据

注意:您使用的URL必须与Disqus中的URL匹配。有关如何可靠地进行设置的信息,请参阅Web集成文档。

示例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('link:' + url);
  });
});
</script>

制作请求API

$('#get-counts-button').click(function () {
  $.ajax({
    type: 'GET',
    url: "https://disqus.com/api/3.0/threads/set.jsonp",
    data: { api_key: disqusPublicKey, forum : disqusShortname, thread : urlArray },
    cache: false,
    dataType: 'jsonp',
    success: function (result) {

      for (var i in result.response) {

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

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

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

      }
    }
  });
});

答案 1 :(得分:3)

Disqus拥有web api,允许开发人员在自己的应用程序中与Disqus数据进行通信。

http://disqus.com/api/docs/

http://disqus.com/api/docs/forums/listThreads/

您也可以使用http://disqus.com/api/console/来测试api

我使用https://github.com/disqus/disqus-php

require('disqusapi/disqusapi.php');
$disqus = new DisqusAPI('yoursecretkey');
print_r($disqus->forums->listThreads(array('forum'=>'your_ shortname')));

答案 2 :(得分:1)

我用这个来获得评论数:

http://help.disqus.com/customer/portal/articles/565624

它会更新您在页面中设置的链接: Second article

链接“第二篇文章”的内容将替换为评论计数。 即“22评论”。比使用ajax用注释计数更新你的db。

答案 3 :(得分: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 )。

希望这有助于某人!

相关问题