2015-06-03 39 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)。

希望这可以帮助别人!