javascript 如何在 Disqus 中获得评论数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9149979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get comment count in Disqus?
提问by Obay
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.
我想将 Disqus 评论数存储在我自己的数据库中,以便我可以按评论数对我的文章进行排序。基本上,每次在我的网站上阅读一个页面时,我都会询问 Disqus 该页面有多少评论,然后用该数量更新数据库。
http://docs.disqus.com/help/3/doesn't seem to be helpful.
http://docs.disqus.com/help/3/似乎没有帮助。
Any suggestions?
有什么建议?
回答by Youssef Subehi
Get comment counts with disqus API
使用 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
注册Disqus API 密钥(可选)拥有自己的站点来替换示例数据
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.
注意:您使用的 URL 必须与 Disqus 中设置的 URL 匹配。有关可靠设置的信息,请参阅 Web 集成文档。
Example HTML
示例 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
制作请求 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>');
}
}
});
});
回答by Nish
Disqus have web api which allows developers to communicate with Disqus data from within their own applications.
Disqus 具有 web api,允许开发人员从他们自己的应用程序中与 Disqus 数据进行通信。
http://disqus.com/api/docs/forums/listThreads/
http://disqus.com/api/docs/forums/listThreads/
Also you can use http://disqus.com/api/console/to test api
您也可以使用http://disqus.com/api/console/来测试 api
I use https://github.com/disqus/disqus-php
我使用 https://github.com/disqus/disqus-php
require('disqusapi/disqusapi.php');
$disqus = new DisqusAPI('yoursecretkey');
print_r($disqus->forums->listThreads(array('forum'=>'your_ shortname')));
回答by Grig
I used this to get the comment count:
我用它来获取评论数:
http://help.disqus.com/customer/portal/articles/565624
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.
链接“第二篇文章”的内容将替换为评论数。即“22 条评论”。比使用 ajax 用评论计数更新你的数据库。
回答by MrLewk
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.
我知道这是一个老问题,但是 Google 提出了很多这样的 SO 问题(这是最高的结果),大多没有任何可靠的答案或依赖于 Github API 的答案,这似乎不太好。
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.
数天以来,我一直在努力获取评论数,并且还尝试了该 API 类,该类似乎因某些致命错误而使我的应用程序崩溃。
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:
经过更多的搜索,我发现了一个指向 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'];
}
You'll need to register an application to get your public API key, which you can do here: https://disqus.com/api/applications/
您需要注册一个应用程序来获取您的公共 API 密钥,您可以在此处完成: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:
该$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
)
)
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]
)
因为数组返回时没有任何有用的顶级数组键,所以我们array_search
通过列名键对数组进行操作,我们将知道:您的评论插件所在的页面 URL ( [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
).
然后这将返回顶级数组键,在这种情况下0
,我们可以将其传回以从数组中提取我们想要的信息,例如总评论(数组键posts
)。
Hope this helps someone!
希望这对某人有帮助!