Skip to content
Created Sync comments (markdown) authored by Roumen Damianoff's avatar Roumen Damianoff
#### Example: How to sync comments from Disqus to your local database
```php
Route::get('sync-comments', function(){
// this is your disqus secret key
$secret_key = 'yoursecretkey';
// create new instance of DisqusAPI
$disqus = new DisqusAPI($secret_key);
// uncomment if you have trouble with secure connections
//$disqus->setSecure(false);
// NOTE: if you don't have posts in the table, you need to set $since to dummy date
// else you need to fix the time format
$since = str_replace(' ', 'T', DB::table('comments')->max('date'));
$params = array(
'forum'=>'yourforumname',
'limit'=>51,
'order'=>'asc',
'include'=>'approved',
'related'=>'thread',
'since'=>$since
);
// get a list with all disqus comments since last comment in your local db
$comments = $disqus->posts->list($params);
// add comments to your db
do
{
if(count($comments) > 1)
{
// unset duplicate comment
unset($comments[0]);
// counter
$n = 0;
// save comments locally
foreach ($comments as $comment)
{
$slug = $comment->thread->identifiers[0];
// NOTE: you may need to manipulate your slug in order to use it later as key
$data = array(
'comment_id' => $comment->id,
'parent_id' => $comment->parent,
'slug' => $slug,
'body' => strip_tags($comment->message),
'author_name' => $comment->author->name,
'profile_url' => $comment->author->profileUrl,
'gravatar_url' => $comment->author->avatar->permalink,
'date' => $comment->createdAt
);
// NOTE: you need appropriated db structure
DB::table('comments')->insert($data);
$n++;
}
echo "<p><strong>Action: </strong> {$n} new comments added to DB</p>";
} else
{
echo "<p><strong>THERE ARE NO NEW COMMENTS</strong></p>";
}
} while (count($comments) == 50);
});
```
\ No newline at end of file