2013-05-02 61 views
0

我找到了一個PHP腳本,我試圖用它來從博客主題中獲取評論,似乎所有評論都存在,但我無法弄清楚它爲什麼不起作用正確。我不斷收到此錯誤從PHP腳本中檢索Disqus發佈的所有評論

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

這裏的腳本:

<?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); 
    }*/ 
} 
} 

?> 

我想也許這是我的API密鑰,但我已經與Disqus和它的其他更基本的腳本檢查了幾次在這些腳本上正常工作。

+1

嘗試用'print_r($ comments)'調試'$ comments'變量並查看它包含的內容。 – 2013-05-02 06:58:50

回答

1

主要問題是'threads/listPosts'API端點需要您指定線程,所以出現了失敗響應。我修復了腳本中的其他潛在問題並使其工作(請參閱下面的代碼)。

請注意,此版本使用您的密鑰。要使用您的公鑰,請將'api_secret'更改爲'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); 
     }*/ 
    } 
} 

?> 
+0

這太棒了,謝謝!雖然我仍然遇到錯誤:注意:嘗試在第35行的/Applications/MAMP/htdocs/CBP.php中獲取非對象的屬性,我認爲這是「已創建」屬性,因爲雖然我獲取了帖子和作者我沒有得到任何回來的時間/日期。 – OSUBrit 2013-05-03 18:11:11

+0

沒關係,我只是改變了評論和CreatedAt的順序,它的工作! – OSUBrit 2013-05-03 18:22:39