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!