Skip to content

Instantly share code, notes, and snippets.

@PonomareVlad
Created December 24, 2020 19:31
Show Gist options
  • Save PonomareVlad/4b2786f4bd2f2d56fff287be92faaa10 to your computer and use it in GitHub Desktop.
Save PonomareVlad/4b2786f4bd2f2d56fff287be92faaa10 to your computer and use it in GitHub Desktop.
<?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;
}
echo parse_disqus_file($_REQUEST['url']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment