2012-02-05 77 views
7

我想將Disqus評論數存儲在我自己的數據庫中,以便按評論數量對我的文章進行排序。基本上,每次在我的網站上閱讀一個頁面時,我都想問問某些頁面有多少條評論,然後用這個數字更新數據庫。如何在Disqus找到評論數?

http://docs.disqus.com/help/3/似乎沒有幫助。

有什麼建議嗎?

+0

您提供的鏈接不再存在。新的頁面是[添加評論計數鏈接到您的主頁](https://help.disqus.com/customer/portal/articles/565624-adding-comment-count-links-to-your-home-page)和似乎非常有幫助。 – 2015-11-20 09:06:23

回答

3

Disqus有web api允許開發人員在他們自己的應用程序中與Disqus數據進行通信。

http://disqus.com/api/docs/

http://disqus.com/api/docs/forums/listThreads/

您也可以使用http://disqus.com/api/console/測試API

我用https://github.com/disqus/disqus-php

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

鑑於評論數量是公開信息,可惜的是,它似乎是不必要的複雜,以獲得它。 – daluege 2014-08-24 14:52:31

4

與disqus API獲取評論數

這裏有你需要什麼開始之前已經完成:

註冊一個Disqus API key (可選)擁有自己的網站,以替換示例數據

注意:您使用的URL必須與Disqus中設置的URL匹配。有關如何可靠地進行設置的信息,請參閱Web集成文檔。

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

變量:

<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('link:' + url); 
    }); 
}); 
</script> 

發出請求API

$('#get-counts-button').click(function() { 
    $.ajax({ 
    type: 'GET', 
    url: "https://disqus.com/api/3.0/threads/set.jsonp", 
    data: { api_key: disqusPublicKey, forum : disqusShortname, thread : urlArray }, 
    cache: false, 
    dataType: 'jsonp', 
    success: function (result) { 

     for (var i in result.response) { 

     var countText = " comments"; 
     var count = result.response[i].posts; 

     if (count == 1) 
      countText = " comment"; 

     $('div[data-disqus-url="' + result.response[i].link + '"]').html('<h4>' + count + countText + '</h4>'); 

     } 
    } 
    }); 
}); 
+0

你能解釋一下url結構嗎:https://disqus.com/api/3.0/threads/set.jsonp?所以我的意思是,當我們想要計數,我們需要設置這樣的網址:https://disqus.com/api/3.0/threads/set.jsonp – 2017-02-19 03:39:44

0

我知道這是一個老問題,但谷歌變成了大量的這些所謂的問題(這是最重要的結果),大多沒有任何可靠的答案或依賴於Github API wh的答案這似乎並不能很好地工作。


我一直努力獲得評論計數天,也試過這似乎與一些致命的錯誤崩潰我的應用程序,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返回的頁面的評論插件的很多信息。例如:

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,我們將知道:你的網頁網址,其中評論插件([link]

然後這將返回頂級數組密鑰,在這種情況下0,然後我們可以傳回以從數組中提取我們想要的信息,例如總註釋(數組鍵posts)。

希望這可以幫助別人!