Open cyruskafaiwu opened 11 years ago
I'll share with you a snipet of the the way i use to get comments from Disqus in a symfony 2 project.
more documentation can be found here : http://disqus.com/api/docs/posts/list/
NB: I'm using Guzzle bundle (like cURL), the instance is '$this->client'
public function fetchComment($thread, $order, $cursor, $limit = 100) { $fetch = 'posts/list.json?{?forum, api_key, limit, order, cursor, thread}'; if ($order == 'popular') { $fetch = 'posts/listPopular.json?{?order, forum, thread, api_key, limit}'; }
$request = $this->client->get(
array(
$this->baseUrl.$fetch,
array(
'api_key' => $this->apiKey,
'limit' => $limit,
'forum' => $this->shortname,
'thread' => $thread,
'cursor' => $cursor,
'order' => $order,
)
)
);
$response = $this->sendRequest($request);
if ($response['response'] !== false) {
foreach ($response['response'] as $key => $comment) {
$treeComments = array();
$this->buildTreeComment($response['response'], $treeComments);
}
$response['response'] = $treeComments;
}
return $response;
}
this is the function to build the tree comments (each answer message is under the first message it's related to)
protected function buildTreeComment(array &$comments, array &$treeComments, $currentParentId = 0) { $now = new \DateTime(); foreach ($comments as $key => $comment) { $cmCreatedAt = new \DateTime($comment['createdAt']); $interval = $cmCreatedAt->diff($now);
if (($diff = $interval->format('%y')) > 0 ) {
$typeDiff = $diff > 1 ? ' ans':' an';
} elseif (($diff = $interval->format('%m')) > 0) {
$typeDiff = 'mois';
} elseif (($diff = $interval->format('%a')) > 0) {
$typeDiff = $diff > 1 ? ' jours':' jour';
} elseif (($diff = $interval->format('%h')) > 0) {
$typeDiff = $diff > 1 ? ' heurs':' heur';
} elseif (($diff = $interval->format('%i')) > 0) {
$typeDiff = $diff > 1 ? ' minutes':' minute';
}
$comment['since'] = sprintf("Il ya %s %s", $diff, $typeDiff);
if ($comment['parent'] == $currentParentId) {
$comment['children'] = array();
$this->buildTreeComment($comments, $comment['children'], $comment['id']);
$treeComments[] = $comment;
}
}
}
hope it helps
Other solution is to modify the library: instead of:
return $data->response;
use
return $data;
that way you will have access to cursor information see https://disqus.com/api/docs/cursors/
PS: their console is broken it send limit instead of cursor so you can't test if this work there.
after removing ->response I use this code to fetch all data.
function save($fname, $obj) {
$f = fopen($fname, 'w');
fwrite($f, json_encode($obj, JSON_PRETTY_PRINT));
fclose($f);
}
require('disqusapi/disqusapi.php');
$disqus = new DisqusAPI('<SECRET>');
function fetch($options, $fn, $cursor = NULL) {
if ($cursor != NULL) {
$payload = array_merge($options, array('cursor' => $cursor));
} else {
$payload = $options;
}
$res = $fn($payload);
$posts = $res->response;
if ($res->cursor->hasNext) {
$posts = array_merge($posts, fetch($options, $fn, $res->cursor->next));
}
return $posts;
}
$opts = array('forum' => '<name>'); // if you have lot of data use 'limit' => 100
save('posts.json', fetch($opts, function($payload) use ($disqus) {
return $disqus->posts->list($payload);
}));
save('threads.json', fetch($opts, function($payload) use ($disqus) {
return $disqus->threads->list($payload);
}));
How do you get all the threads listed?
I can't figure out the cursor.
Here's my code: function listThreads($cursor,$incurrences) { global $setting; global $disqus; $incurrences = $incurrences+1; $params = array('forum' => $setting['disqus_forum'], 'order' => 'desc', 'limit' => 90); if($cursor != 0) { $params['cursor'] = $cursor; } echo $cursor.'
'; $i = 0; $threads = $disqus->threads->list($params); $cursor = $cursor+90; foreach ($threads as $thread) { $i = $i+1; $id = $thread->id; $title = $thread->title; $link = $thread->link; $date = date('Y-m-d H:i:s',strtotime($thread->createdAt."+0000")); if(mysql_num_rows(mysql_query("SELECT * FROM
disqus_threads
wherethread_id
= '$id' andtitle
= '$title'")) == 0) { $insert = mysql_query("INSERT INTOdisqus_threads
(thread_id
,title
,link
,date
) VALUES ('$id','$title','$link','$date')") or die(mysql_error()); } }