Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tool to parse disqus comments xml file
<?php
/*
* Tool to parse disqus comments xml file.
*
* @author Prahlad Yeri<prahladyeri@yahoo.com>
* @date 2017-09-06
* */
function find_url($root, $thid) {
foreach($root->thread as $thread) {
$curr_id = $thread->attributes("dsq", true)[0];
if (strcmp($thid,$curr_id) === 0) {
$url = $thread->id;
return $url;
}
}
return null;
}
function parse_disqus_file($filename) {
$xmlstr = file_get_contents($filename);
$root = new SimpleXMLElement($xmlstr);
$posts = $root->post;
$cnt = 0;
$result = [];
foreach($posts as $post) {
$thid = "";
foreach($post->thread->attributes("dsq",true) as $a=>$b) {
$thid = $b;
break;
}
$url = find_url($root, $thid);
$values = array(
"body"=>strip_tags($post->message),
"name"=>$post->author->name,
"email"=>$post->author->email,
//"created_at"=>date("Y-m-dTH:i:sZ", $post->created_at),
//"created_at"=>date_create($post->createdAt),
"created_at"=>$post->createdAt,
"url"=>$url,
);
array_push($result, $values);
$cnt++;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.