我找到了一個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和它的其他更基本的腳本檢查了幾次在這些腳本上正常工作。
嘗試用'print_r($ comments)'調試'$ comments'變量並查看它包含的內容。 – 2013-05-02 06:58:50