How to get the number of comments in Disqus?

I would like to keep the Disqus comment counter in my own database so that I can sort my articles by the number of comments. Basically, every time a page is read on my site, I would like to ask Disqus how many comments this page has, and then update the database with this account.

http://docs.disqus.com/help/3/ does not seem useful to you.

Any suggestions?

+9
source share
4 answers

Get the number of comments using the disqus API

Here's what you need to do before you get started:

Register to receive Disqus API key (optional) Create your own site to replace sample data

NOTE. The URL you use must match the one specified in Disqus as the URL. For reliable configuration, see the web integration documentation.

HTML example

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

Create 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>'); } } }); }); 
+8
source

Disqus has a web api that allows developers to communicate with Disqus data from 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 am using https://github.com/disqus/disqus-php

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

I used this to count comments:

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

It updates the link provided on the page: Second article

The content of the Second Article link will be replaced by a comment. that is, "22 comments." Instead of using ajax for updating, you are using db with comment counting.

+1
source

I know this is an old question, but Google reveals a lot of these SO questions (this is the main result), mostly without any solid answers or answers that rely on the Github API, which doesn't seem to work very well.


I struggled to count the number of comments over the course of a few days, and also tried this 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 conversation I wrote a quick function to get a comment counter:

 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 need to register the application in order to get a public API key that you can do here: https://disqus.com/api/applications/

This function then simply displays the total number of comments that you can save to the database or something else.

What this function does:

The $json array returns a lot of information about the page where your comment plugin is enabled. 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 ) ) 

Since the array is returned without any useful keys for the top-level array, we do array_search in the array using the column name we will know: the URL of your page, where the comment plugin ( [link] )

Then the top-level array key will be returned, in this case 0 , which we can pass back to extract the information we need from the array, such as general comments ( posts key array).

Hope this helps someone!

+1
source

All Articles