我希望将 Disqus 评论计数存储在我的数据库中,以便我可以根据评论计数对文章进行排序。基本上,每当某个页面在我的网站上被阅读时,我想问 Disqus 这个页面有多少评论,然后更新数据库中的计数。 http://docs.disqus.com/help/3/ 看起来并没有什么帮助。请问有什么建议吗?
使用disqus API获取评论计数
在开始之前,您需要完成以下操作:
注册Disqus API密钥 (可选)拥有自己的网站以替换示例数据
注意:您使用的URL必须与Disqus中设置的URL匹配。请参阅Web Integration文档,了解如何可靠地设置此项。
示例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>
$('#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>');
}
}
});
});
thread:link
参数,而不是一个包含字符串数组的线程参数,格式为“link:https://example.com”。所以我将 'thread:link='+url
推入数组中,然后使用 &
连接该数组。 - fvrabhttp://disqus.com/api/docs/forums/listThreads/
Also you can use http://disqus.com/api/console/ to test the API.require('disqusapi/disqusapi.php');
$disqus = new DisqusAPI('yoursecretkey');
print_r($disqus->forums->listThreads(array('forum'=>'your_ shortname')));
http://help.disqus.com/customer/portal/articles/565624
它会更新您在页面中设置的链接: 第二篇文章
链接“第二篇文章”的内容将被替换为评论计数。 即“22条评论”。然后使用ajax更新您的数据库以显示评论计数。
我知道这是一个老问题,但是谷歌搜索出了很多这样的SO问题(这是排名第一的结果),大多数没有实质性的答案或者依靠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
)。