2015-06-03 40 views
1

這似乎是一個持續的問題,其他人以及我..但即使在「問題,可能已經有你的答案」發佈時,我仍然無法得到這個工作。使用PHP獲取面板評論數

我真正想要的是通過傳遞文章的標識符(或URL)給定文章的評論數。

我從github上下載了這個:https://github.com/disqus/disqus-php

跟着這裏的第一個答案:How to get Disqus comment count for a page using Disqus PHP API?這似乎最接近我試圖實現。這使我這個遠:

<?php 
    require_once('scripts/disqusapi/disqusapi.php'); 
    $disqus = new DisqusAPI('secret_key'); 
    $page_with_comments = $disqus->posts->details(array('thread'=>"LINK-IDENTIFIER")); 
    $comment_count = $page_with_comments->posts; 
?> 

當我做到這一點,但是我正在此錯誤:

Fatal error: Uncaught exception 'Exception' with message 'Missing required argument: post' in /home/mugheads/public_html/scripts/disqusapi/disqusapi.php:82 Stack trace: #0 /home/mugheads/public_html/test.php(4): DisqusResource->__call('details', Array) #1 /home/mugheads/public_html/test.php(4): DisqusResource->details(Array) #2 {main} thrown in /home/mugheads/public_html/scripts/disqusapi/disqusapi.php on line 82 

此錯誤表明您可以不再使用「線」來檢索JSON需要..我無法找到ID Disqus使用的帖子,因爲使用「post」就像錯誤描述只接受一個整數。

如果有人知道更簡單的方法,或絕對有效的方式,請分享!

任何幫助將不勝感激!

+0

也許用'post'取代'thread' – Augwa

+0

感謝您的回覆!當我這樣做,它告訴我發佈必須是一個整數..但沒有辦法我的知識使用身份證的,而不是文字來識別帖子。 – Zephni

回答

1

我知道這是一個古老的問題,但谷歌提出了很多這樣的問題,大多沒有任何可靠的答案或答案,依賴於這個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,我們將知道:你的網頁網址,其中評論插件([link]

然後,這將返回頂級陣列的關鍵,在這種情況下0然後我們可以回傳給我們提取從數組,想要的信息總量意見,如(數組鍵posts)。

希望這可以幫助別人!