10

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?

1

4 Answers 4

8

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

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>');
          }
        }
    });
});
3
  • Can you explain the url structure: disqus.com/api/3.0/threads/set.jsonp ? so does i mean when we want to get count we need to set the url like this: disqus.com/api/3.0/threads/set.jsonp Commented Feb 19, 2017 at 3:39
  • Thanks for your answer but I am getting the count of only 5 posts in response. Is there any limit thing or should I send the request in batches for 5 posts? What do you suggest? Commented Jul 30, 2018 at 11:25
  • something has probably changed in disqus api, becasue i have to send multiple thread:link params instead of one thread parameter wich includes array of strings in "link:example.com" format.. so i push 'thread:link='+url to array and then join this array with &
    – fvrab
    Commented Sep 25, 2019 at 9:05
3

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')));
1
  • 10
    Given that the comment count is a public information, it's a pity that it seems to be unnecessarily complicated to obtain it.
    – filip
    Commented Aug 24, 2014 at 14:52
1

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.

1

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!

Not the answer you're looking for? Browse other questions tagged or ask your own question.