function list100Threads($endpoint, $cursor, $cursorMaxCount, $limit)
{
    global $apikey, $cursorMaxCount, $num_deleted_posts, $num_results_checked;
    // create a new cURL resource
    $session = curl_init($endpoint . $cursor);
    // set URL and other appropriate options
    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
    // instead of just returning true on success, return the result on success
    // set threads info
    $data = curl_exec($session);
    // close cURL resource, and free up system resources
    curl_close($session);
    // decode the json data to make it easier to parse the php
    $results = json_decode($data);
    if ($results === NULL) {
        die('No data has been gathered.');
    }
    // grab threads
    $posts = $results->response;
    // grab the current cursorMaxCount
    $cursor = $results->cursor;
    /* What the cursor array looks like:
    		"cursor": {
    		    "prev": null,
    		    "hasNext": true,
    		    "next": "1320136178861708:0:0",
    		    "hasPrev": false,
    		    "total": null,
    		    "id": "1320136178861708:0:0",
    		    "more": true
    		} */
    //Start from the first response.
    $post_checked = 0;
    //Start from the response directly after the first.
    $post_checked_against = 1;
    //Cycle through the responses: Run while we're under the response limit and we have valid Post IDs to check against.
    while ($post_checked < $limit && $posts[$post_checked_against]->id !== NULL) {
        echo '<h3>Post being checked - ' . $post_checked_against . '</h3>';
        //Check against other responses: Run while we're not equal to the limit and we still have valid Post IDs to check against.
        while ($post_checked_against !== $limit && $posts[$post_checked_against]->id !== NULL) {
            echo '<p>Comparing ' . $posts[$post_checked]->id . ' with ' . $posts[$post_checked_against]->id . '</p>';
            //If the messages of these two responses are the same posted by the same username, delete the Post ID being checked against vs. the one cycled through.
            if ($posts[$post_checked]->message === $posts[$post_checked_against]->message && $posts[$post_checked]->author->username === $posts[$post_checked_against]->author->username) {
                $getThreadDetails = 'https://disqus.com/api/3.0/posts/remove?api_secret=' . $apikey . '&post=' . $posts[$post_checked_against]->id;
                $threadDetailsSession = curl_init($getThreadDetails);
                curl_setopt($threadDetailsSession, CURLOPT_POST, 1);
                curl_setopt($threadDetailsSession, CURLOPT_POSTFIELDS, '');
                curl_setopt($threadDetailsSession, CURLOPT_RETURNTRANSFER, 1);
                // prevents the output from being displayed on the pgae
                $data = curl_exec($threadDetailsSession);
                curl_close($threadDetailsSession);
                echo '<h4>Same comment by the same author. Post ' . $posts[$post_checked_against]->id . ' was deleted.</h4>';
                $num_deleted_posts++;
            }
            //Cycling through the posts being checked against.
            $post_checked_against++;
        }
        //Increment in order to pass up what has already been checked and cycle through what is left.
        $post_checked++;
        $post_checked_against = $post_checked + 1;
        //Tracking the number of Posts checked.
        $num_results_checked++;
    }
    //Getting the next page of results if we hit the limit.
    if ($post_checked_against === $limit) {
        $cursor = $cursor->next;
        echo '<h4>' . $num_results_checked . ' processed. On to the next batch.</h4>';
        list100Threads($endpoint, $cursor, $cursorMaxCount, $limit);
        /* uncomment to only run $cursorMaxCount number of iterations
        		$cursorMaxCount++;
        		if ($cursorMaxCount < 10) {
        			list100Threads($endpoint,$cursor,$cursorMaxCount);
        		}*/
    }
}
function list100Threads($endpoint, $cursor, $cursorMaxCount)
{
    global $apikey, $totalThreads, $closedThreads;
    // create a new cURL resource
    $session = curl_init($endpoint . $cursor);
    // set URL and other appropriate options
    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
    // instead of just returning true on success, return the result on success
    // set threads info
    $data = curl_exec($session);
    // close cURL resource, and free up system resources
    curl_close($session);
    // decode the json data to make it easier to parse the php
    $results = json_decode($data);
    if ($results === NULL) {
        die('Error parsing JSON');
    }
    // grab threads
    $threads = $results->response;
    // grab the current cursor
    $cursor = $results->cursor;
    /* What the cursor array looks like:
    		"cursor": {
    		    "prev": null,
    		    "hasNext": true,
    		    "next": "1320136178861708:0:0",
    		    "hasPrev": false,
    		    "total": null,
    		    "id": "1320136178861708:0:0",
    		    "more": true
    		}
    	*/
    $i = 0;
    foreach ($threads as $thread) {
        $id = $thread->id;
        echo "Processing thread " . $id . "... ";
        // first let's find out if the thread is already open or not
        $getThreadDetails = 'https://disqus.com/api/3.0/threads/details?api_secret=' . $apikey . '&thread=' . $id;
        $threadDetailsSession = curl_init($getThreadDetails);
        curl_setopt($threadDetailsSession, CURLOPT_RETURNTRANSFER, 1);
        // instead of just returning true on success, return the result on success
        $data = curl_exec($threadDetailsSession);
        curl_close($threadDetailsSession);
        // decode the json data to make it easier to parse the php
        $result = json_decode($data);
        if ($result === NULL) {
            die('Error parsing JSON');
        }
        $thread = $result->response;
        if ($thread->isClosed == false) {
            echo "Thread is already open. Skipping.<br />";
        } else {
            // open the thread
            $openThread = 'https://disqus.com/api/3.0/threads/open?api_secret=' . $apikey . '&thread=' . $id;
            $openThreadSession = curl_init($openThread);
            curl_setopt($openThreadSession, CURLOPT_POST, 1);
            curl_setopt($openThreadSession, CURLOPT_POSTFIELDS, '');
            curl_setopt($openThreadSession, CURLOPT_RETURNTRANSFER, 1);
            // prevents the output from being displayed on the pgae
            $result = curl_exec($openThreadSession);
            curl_close($openThreadSession);
            echo "Thread was closed. Opened.<br />";
            $closedThreads++;
        }
        $totalThreads++;
        $i++;
    }
    // cursor through until today
    if ($i == 100) {
        $cursor = $cursor->next;
        $i = 0;
        list100Threads($endpoint, $cursor);
        /* uncomment to only run $cursorMaxCount number of iterations
        		$cursorMaxCount++;
        		if ($cursorMaxCount < 10) {
        			list100Threads($endpoint,$cursor,$cursorMaxCount);
        		}*/
    }
}
function list100Threads($endpoint, $cursor, $j)
{
    //echo "Endpoint is ".$endpoint."<br />";
    //echo "Cursor is ".$cursor;
    // create a new cURL resource
    $session = curl_init($endpoint . $cursor);
    // set URL and other appropriate options
    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
    // instead of just returning true on success, return the result on success
    // set threads info
    $data = curl_exec($session);
    // close cURL resource, and free up system resources
    curl_close($session);
    // decode the json data to make it easier to parse the php
    $results = json_decode($data);
    if ($results === NULL) {
        die('Error parsing json');
    }
    // grab threads
    $threads = $results->response;
    // grab the current cursor
    $cursor = $results->cursor;
    //var_dump($cursor);
    /* What the cursor array looks like:
    		"cursor": {
    		    "prev": null,
    		    "hasNext": true,
    		    "next": "1320136178861708:0:0",
    		    "hasPrev": false,
    		    "total": null,
    		    "id": "1320136178861708:0:0",
    		    "more": true
    		}
    	*/
    //echo "<ul>";
    $i = 0;
    foreach ($threads as $thread) {
        $url = $thread->link;
        $count = $thread->posts;
        $created = $thread->createdAt;
        //echo "<li>".$created." ".$url." ".$count."</li>";
        // output to csv
        header('Content-type: text/csv');
        header('Content-disposition: attachment; filename="threads.csv"');
        $fp = fopen('php://output', 'w+');
        fwrite($fp, $created);
        fwrite($fp, ",");
        fwrite($fp, $url);
        fwrite($fp, ",");
        fwrite($fp, $count);
        fwrite($fp, "\r\n");
        fclose($fp);
        $i++;
    }
    //echo "</ul>";
    // cursor through until today
    if ($i == 100) {
        $cursor = $cursor->next;
        $i = 0;
        list100Threads($endpoint, $cursor);
        /* uncomment to only run $j number of iterations
        		$j++;
        		if ($j < 10) {
        			list100Threads($endpoint,$cursor,$j);
        		}*/
    }
}