Disqus API包含使用在JSON响应中返回的游标的分页方法。看到这里有关游标的信息:http://disqus.com/api/docs/cursors/
既然你提到的PHP,这样的事情应该让你开始:
<?php
$apikey = '<your key here>'; // get keys at http://disqus.com/api/ — can be public or secret for this endpoint
$shortname = '<the disqus forum shortname>'; // defined in the var disqus_shortname = '...';
$thread = 'link:<URL of thread>'; // IMPORTANT the URL that you're viewing isn't necessarily the one stored with the thread of comments
//$thread = 'ident:<identifier of thread>'; Use this if 'link:' has no results. Defined in 'var disqus_identifier = '...';
$limit = '100'; // max is 100 for this endpoint. 25 is default
$endpoint = 'https://disqus.com/api/3.0/threads/listPosts.json?api_key='.$apikey.'&forum='.$shortname.'&limit='.$limit.'&cursor='.$cursor;
$j=0;
listcomments($endpoint,$cursor,$j);
function listcomments($endpoint,$cursor,$j) {
// Standard CURL
$session = curl_init($endpoint.$cursor);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); // instead of just returning true on success, return the result on success
$data = curl_exec($session);
curl_close($session);
// Decode JSON data
$results = json_decode($data);
if ($results === NULL) die('Error parsing json');
// Comment response
$comments = $results->response;
// Cursor for pagination
$cursor = $results->cursor;
$i=0;
foreach ($comments as $comment) {
$name = $comment->author->name;
$comment = $comment->message;
$created = $comment->createdAt;
// Get more data...
echo "<p>".$name." wrote:<br/>";
echo $comment."<br/>";
echo $created."</p>";
$i++;
}
// cursor through until today
if ($i == 100) {
$cursor = $cursor->next;
$i = 0;
listcomments($endpoint,$cursor);
/* uncomment to only run $j number of iterations
$j++;
if ($j < 10) {
listcomments($endpoint,$cursor,$j);
}*/
}
}
?>
非常感谢!但是,我们需要准确地为$ thread(线程的URL)和$ cursor?顺便说一句,我们最多只能有100条评论吗? – 2013-02-26 09:15:23
线程的URL只是注释页面的URL。在这种情况下,它是http://www.cnn.com/2013/02/25/tech/innovation/google-glass-privacy-andrew-keen/index.html - 游标值从API响应中提取,并且代表下一组100条评论。该脚本将一直持续到没有其他评论为止。 – 2013-02-27 05:39:14
我将$ shortname设置为'cnn'(var disqus_shortname ='cnn';)和$ thread''链接:'并保持$ cursor为空,但事实证明“Error parsing json”。我想念什么? –
2013-02-27 07:11:14