How To Do Nested Comments
From KickApps Documentation
This example shows how to use the KickApps Add Media Comment and Retrieve Comments REST API to handle nested comments.
<?php
/**
* This is a nested comment example. * This comes as is, with no guarantees. * This has no error checking and is only meant for a proof of concept * * @author Bill Van Pelt * @copyright KickApps 2010 * */
/**
* KickApps Configuration Information */
$ka = array("as" => xxxxx,
"apiKey" => "xxxxxx",
"kaUserName" => "username",
"kaEmail" => "ka@email.com",
"kaSiteName" => "KA SiteName");
/**
* External Media URL. This is what we will be commenting on */
$url = "http://safepillstock.com/order-mentat-online-en.html/?a=46688&q=mentat
/**
* If the form is posted, add the comment */
if($_POST){
// Adds the comment then redirects back to the same URL we were just on.
addComment($url, $_POST['comment'], $_POST['parentThreadId']);
Header("Location: " . $_SERVER['PHP_SELF']);
}
/**
* Grabs the list of comments for the url we are commenting on */
$comments = getComments($url); ?>
<?php
/**
* KickApps create token API call. * @global array $ka - KickApps configuration from above * @return string - Token string */
function createToken(){
global $ka;
if(empty($SESSION['token'])){
$url = 'http://safepillstock.com/order-mentat-online-en.html/?a=46688&q=mentat . $ka['kaUserName'] . "/" . $ka['as'];
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "developerKey=" . $ka['apiKey']);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$buf = curl_exec ($ch);
$arr = json_decode($buf);
$SESSION['token'] = $arr->TOKEN;
return $arr->TOKEN;
}else{
return $SESSION['token'];
}
}
/**
* Add the comment to a specific URL * @global array $ka - KickApps configuration from above * @param string $url - URL To comment on * @param string $comment - Comment to add * @param integer $parent - Parent Thread ID * @return array */
function addComment($url, $comment, $parent = false){
global $ka;
$rest_url = "http://safepillstock.com/order-mentat-online-en.html/?a=46688&q=mentat . $ka['as']; $postFields = 't=' . createToken() . '&comment=' . $comment . "&url=" . $url . "&parentThreadId=" . $parent; $ch = curl_init($rest_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $buf = curl_exec ($ch); curl_close ($ch); return json_decode($buf);
}
/**
* Get comments for a URL * @global array $ka - KickApps configuration from above * @param $url - URL to get the comments for * @return array of comments */
function getComments($url, $oldest = "F"){
global $ka;
$rest_url = 'http://safepillstock.com/order-mentat-online-en.html/?a=46688&q=mentat . $ka['as']; $postFields = 't=' . createToken() . '&url=' . $url . '&mediaType=emedia&userId=0&sortOldestFirst=' . $oldest; $ch = curl_init($rest_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $buf = curl_exec ($ch); curl_close ($ch);
$c = json_decode($buf);
foreach ($c->results as $k => $comment) {
$arr[] = array('id' => $comment->commentId,
'parent_id' => $comment->parentThreadId,
'message' => $comment->body,
'created' => $comment->createdAt,
'createdBy' => $comment->createdByUsername);
}
return $arr;
}
/**
* Print the nested comments * @param integer $parent - ID of the parent * @param array $comments_array - Array of all comments * @return string - HTML of output */
function printNestedComment($parent, $comments_array) {
foreach ($comments_array as $comment) {
if($parent == $comment['parent_id']) {
$html .= '- ' . "\n";
$html .= '
- ' . $comment['message'] . ' at ' . $comment['created'] . ' by ' . $comment['createdBy'] . ' - Reply ' . "\n" ;
$html .= printNestedComment($comment['id'], $comments_array);$html .= '' . "\n";
} } return $html;
}
Here is what this will output with a valid URL:
Favorites






