2012-02-05 11 views
7

Disqusのコメント数を自分のデータベースに保存したいので、コメント数で記事を並べ替えることができます。基本的には、自分のサイトでページが読み込まれるたびに、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)。十分に役に立つと思われる。 –

答えて

3

Disqusには、開発者が独自のアプリケーション内でDisqusデータと通信できるWeb APIがあります。

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

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

また、あなたは私が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

1

を使用するAPI

をテストするためにhttp://disqus.com/api/console/を使用することができ、私はコメント数を取得するためにこれを使用:

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

それはだが、ページに設定されたリンクを更新します。 Second article

リンク「第二アーティクル」の内容は、コメント数に置き換えられます。 、すなわち「22コメント」。あなたはajaxを使用してコメント数をdbに更新します。ディスカスのAPI

4

のGetコメント数ここであなたが開始する前に行っている必要がありますものです:

登録Disqus API key (別売)用例データ

を置き換えるために自分のサイトを持っています

注:使用する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&#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?私たちがカウントを取得したいときは、次のようなURLを設定する必要があります:https://disqus.com/api/3.0/threads/set.jsonp –

0

を作り、私はこれが古い質問ですけど、GoogleはこれらのSOの質問の多くを折り返します(これは最上位の結果です)、Github API whに依存する固い答えや答えはほとんどありませんichはうまく動作しないようです。


私は日のコメント数を取得するために苦労し、また、いくつかの致命的なエラーで自分のアプリケーションをクラッシュさせるように見えたその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']; 
    } 

にあなたをhttps://disqus.com/api/applications/

この関数は、データベースに格納できるコメントの総数などを出力するだけです。公開キーを取得するには、アプリケーションを登録する必要があります。

この関数は何:

$json配列は、あなたのコメントプラグインがオンになっているページに関する多くの情報を返します。コメントプラグインは([link])ここで、あなたのページのURL:たとえば:任意の有用なトップレベルの配列キーのない配列を返すため

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を行います

これはトップレベルの配列キーを返します。この場合は0です。このコメントは、合計コメント(配列キーposts)など、配列から必要な情報を抽出するために返すことができます。

これが誰かを助けることを願っています!

関連する問題