1

I have a PHP script I found and I am trying to use to get comments from a blog thread, it seems to all be there, but I can't work out why it's not working correctly. I keep getting this error

Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/AP-Get.php on line 33

Here's the script:

<?php
    ini_set('display_errors', 'on');
    $key="KEY-OMITTED";
    $forum="amandapalmer";
    $thread = '1009158814';
    $limit = '100';

$endpoint = 'http://disqus.com/api/3.0/threads/listPosts.json?api_key='.urlencode($key).'&forum='.$forum.'&limit='.$limit.'&cursor='.$cursor;

$j=0;
listcomments($endpoint,$cursor,$j);

function listcomments($endpoint,$cursor,$j) {

// Standard CURL
$session = curl_init($endpoint.$cursor);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($session);
curl_close($session);

// Decode JSON data
$results = json_decode($data);
if ($results === NULL) die('Error parsing json');

// Comment response
$comments = $results->response;

// Cursor for pagination
$cursor = $results->cursor;

$i=0;
foreach ($comments as $comment) {
    $name = $comment->author->name;
    $comment = $comment->message;
    $created = $comment->createdAt;
    // Get more data...

    echo "<p>".$name." wrote:<br/>";
    echo $comment."<br/>";
    echo $created."</p>";
    $i++;
}

// cursor through until today
if ($i == 100) {
    $cursor = $cursor->next;
    $i = 0;
    listcomments($endpoint,$cursor);
    /* uncomment to only run $j number of iterations
    $j++;
    if ($j < 10) {
        listcomments($endpoint,$cursor,$j);
    }*/
}
}

?>

I thought maybe it was my API key, but I've checked it a few times with other more basic scripts for Disqus and it works fine on those scripts.

OSUBrit
  • 13
  • 3

1 Answers1

1

The main issue was that the 'threads/listPosts' API endpoint requires you to specify the thread, so there was a failure response. I fixed some other potential issues with the script and got it working (see code below).

Note that this version uses your secret key. To use your public key, change 'api_secret' to 'api_key'.

<?php
    ini_set('display_errors', 'on');
    $key="API-SECRET-KEY";
    $forum="amandapalmer";
    $thread = '1009158814';
    $limit = '100';

    $endpoint = 'http://disqus.com/api/3.0/threads/listPosts.json?api_secret='.urlencode($key).'&forum='.$forum.'&limit='.$limit.'&thread='.$thread;

    $j=0;
    listcomments($endpoint,$cursor,$j);

    function listcomments($endpoint,$cursor,$j) {

    // Standard CURL
    $session = curl_init($endpoint.$cursor);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($session);
    curl_close($session);

    // Decode JSON data
    $results = json_decode($data);
    if ($results === NULL) die('Error parsing json');

    // Comment response
    $comments = $results->response;

    // Cursor for pagination
    $cursor = '&cursor=' . $results->cursor->next;

    $i=0;
    foreach ($comments as $comment) {
        $name = $comment->author->name;
        $comment = $comment->message;
        $created = $comment->createdAt;
        // Get more data...

        echo "<p>".$name." wrote:<br/>";
        echo $comment."<br/>";
        echo $created."</p>";
        $i++;
    }

    // cursor through until today
    if ($i == 100) {
        $cursor = $cursor->next;
        $i = 0;
        listcomments($endpoint,$cursor,$j);
        /* uncomment to only run $j number of iterations
        $j++;
        if ($j < 10) {
            listcomments($endpoint,$cursor,$j);
        }*/
    }
}

?>
Ryan V
  • 3,100
  • 1
  • 17
  • 11
  • This is great, thanks! Although I still get an error: Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/CBP.php on line 35. which I think is the "created" property, because while I get the posts and authors I don't get anything back for the time/date. – OSUBrit May 03 '13 at 18:11
  • Nevermind, I just changed the order of comments and CreatedAt and it worked! – OSUBrit May 03 '13 at 18:22