Login   Register  
PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Arturs Sosins  >  PHP Disqus Comments  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: PHP Disqus Comments
Integration of blog comments to import in Disqus
Author: By
Last change:
Date: 2013-03-24 02:48
Size: 3,398 bytes
 

Contents

Class file image Download
<?php
/************************************************************* 
 * This script is developed by Arturs Sosins aka ar2rsawseen, http://webcodingeasy.com 
 * Feel free to distribute and modify code, but keep reference to its creator 
 * 
 * Disqus Import class can generate XML file from provided information
 * about articles and comments, to be imported in your Disqus commenting system.
 * This helps to transfer comments from any existing system to Disqus.
 * 
 * For more information, examples and online documentation visit:  
 * http://webcodingeasy.com/PHP-classes/Import-comments-in-Disqus
**************************************************************/

date_default_timezone_set("Europe/Helsinki");

//usually you would retrieve it from database, but...

//here we have some articles on our blog
$articles = array(
    
=> array(
        
"title" => "This is a cool article about Life",
        
"link" => "http://awesomewebsite.com/article1",
        
"article_text" => "This website does this and that. How do you like it?"
    
),
    
=> array(
        
"title" => "Another cool article",
        
"link" => "http://awesomewebsite.com/article2",
        
"article_text" => "This article is also so cool!"
    
)
);

//and here we have some comments
$comments = array(
    array(
        
"ID" => 1,
        
"username" => "somedude",
        
"email" => "somedude@gmail.com",
        
"pub_date" => 1364114398,
        
"comment" => "Cool website",
        
"status" => "active",
        
"articleID" => 1
    
),
    array(
        
"ID" => 2,
        
"username" => "somechick",
        
"email" => "somechick@gmail.com",
        
"pub_date" => 1364114398,
        
"comment" => "Completely agree",
        
"status" => "active",
        
"articleID" => 1,
        
"repliedTo" => 1
    
),
    array(
        
"ID" => 3,
        
"username" => "someguy",
        
"email" => "someguy@gmail.com",
        
"pub_date" => 1364114398,
        
"comment" => "Also cool article ;)",
        
"status" => "active",
        
"articleID" => 2
    
)
);

//import importer (no joke intented :) )
include("./disqus_import.php");

//create instance of generator
$d = new Disqus\Generator();

//here we will store instances of articles
$arts = array();

//loop through all the comments
foreach($comments as $comment)
{
    
//if there is no instance for this article
    
if(!isset($arts[$comment["articleID"]]))
    {
        
//create instance
        
$arts[$comment["articleID"]] = new Disqus\Article();
        
        
//provide information about article
        
$arts[$comment["articleID"]]->set_title($articles[$comment["articleID"]]["title"]);
        
$arts[$comment["articleID"]]->set_link($articles[$comment["articleID"]]["link"]);
        
$arts[$comment["articleID"]]->set_contents($articles[$comment["articleID"]]["article_text"]);
        
        
//add article to generator
        
$d->add_article($arts[$comment["articleID"]]);
    }
    
    
//create comment instance
    
$com = new Disqus\Comment();
    
    
//provide information about comment
    
$com->set_id($comment["ID"]);
    
$com->set_author($comment["username"]);
    
$com->set_author_email($comment["email"]);
    
$com->set_comment_date(date("Y-m-d H:i:s"$comment["pub_date"]));
    
$com->set_content($comment["comment"]);
    if(
$comment["status"] == "active")
    {
        
$com->set_approved(1);
    }
    else
    {
        
$com->set_approved(0);
    }
    if(isset(
$comment["repliedTo"]))
    {
        
$com->set_parent($comment["repliedTo"]);
    }
    
    
//add comment to article
    
$arts[$comment["articleID"]]->add_comment($com);
}

//output xml
echo $d->generate();
?>