如何获取 Disqus 中的评论数?

发布于 2025-01-03 10:09:00 字数 218 浏览 4 评论 0原文

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

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

有什么建议吗?

I'd like to store the Disqus comment count on my own database, so that I can sort my articles by comment count. Basically, every time a page is read on my site, I'd like to ask Disqus how many comments that certain page has, then update the database with that count.

http://docs.disqus.com/help/3/ doesn't seem to be helpful.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡看悲欢离合 2025-01-10 10:09:00

使用 disqus API 获取评论计数

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

注册 Disqus API 密钥< /a>
(可选)使用您自己的站点来替换示例数据

注意:您使用的 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’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>');
          }
        }
    });
});

Get comment counts with disqus API

Here's what you'll need to have done before starting:

Register for a Disqus API key
(optional) Have your own site to replace the example data

NOTE: The URL you use must match what's set as the URL in Disqus. See Web Integration docs for information on setting this up reliably.

Example 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’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>

Variables:

<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>

Making the Request 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>');
          }
        }
    });
});
一抹淡然 2025-01-10 10:09:00

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

Disqus have web api which allows developers to communicate with Disqus data from within their own applications.

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

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

Also you can use http://disqus.com/api/console/ to test api

I use https://github.com/disqus/disqus-php

require('disqusapi/disqusapi.php');
$disqus = new DisqusAPI('yoursecretkey');
print_r($disqus->forums->listThreads(array('forum'=>'your_ shortname')));
故笙诉离歌 2025-01-10 10:09:00

我用它来获取评论计数:

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

它更新了您在页面中设置的链接:
第二篇文章

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

I used this to get the comment count:

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

It's updates a link you set in page:
Second article

The content of the link 'Second article' will be replaced with the comment count.
i.e "22 Comments". Than use ajax to update you're db with the comment count.

败给现实 2025-01-10 10:09:00

我知道这是一个老问题,但谷歌提出了很多这样的问题(这是最重要的结果),大多数都没有任何可靠的答案或依赖于 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)。

希望这对某人有帮助!

I know this is an old question, but Google turns up plenty of these SO questions (this being the top result), mostly without any solid answers or answers which rely on the Github API which doesn't seem to work very well.


I had been struggling to get the comment count for days, and also tried that API class which seemed to crash my application with some fatal error.

After a bit more searching, I came across a link to the JSON output of the Disqus API, and after some playing around, I wrote a quick function to get the comment count:

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'];
    }

You'll need to register an application to get your public API key, which you can do here: https://disqus.com/api/applications/

This function will then just output the total number of comments which you can then store in the database or whatever.

What this function does:

The $json array returns much information about the page your comment plugin is on. For example:

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
 )
)

Because the array returns without any useful top level array keys, we do an array_search on the array by a column name key which we will know: your page URL where the comments plugin is ([link])

This will then return the top level array key, in this case 0 which we can then pass back to extract the information we want from the array, such as the total comments (array key posts).

Hope this helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文