使用PHP脚本从Disqs检索所有注释
我找到了一个PHP脚本,我正试图用它从一个博客线程中获取评论,它似乎都在那里,但我不知道为什么它不能正常工作。我一直在犯这个错误使用PHP脚本从Disqs检索所有注释,php,disqus,Php,Disqus,我找到了一个PHP脚本,我正试图用它从一个博客线程中获取评论,它似乎都在那里,但我不知道为什么它不能正常工作。我一直在犯这个错误 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"; $fo
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密钥,但我已经用其他更基本的discus脚本检查了几次,它在这些脚本上运行良好。主要问题是“threads/listPosts”API端点要求您指定线程,因此出现了故障响应。我修复了脚本的其他一些潜在问题,并使其正常工作(请参阅下面的代码) 请注意,此版本使用您的密钥。若要使用公钥,请将“api\U密钥”更改为“api\U密钥”
<?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);
}*/
}
}
?>
主要问题是“threads/listPosts”API端点要求您指定线程,因此出现了故障响应。我修复了脚本的其他一些潜在问题,并使其正常工作(请参阅下面的代码) 请注意,此版本使用您的密钥。若要使用公钥,请将“api\U密钥”更改为“api\U密钥”
<?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);
}*/
}
}
?>
试着用打印($comments)
调试$comments
变量,看看它包含什么。试着用打印($comments)
调试$comments
变量,看看它包含什么。这很好,谢谢!尽管我仍然得到一个错误:注意:试图在第35行的/Applications/MAMP/htdocs/CBP.php中获取非对象的属性。我认为这是“created”属性,因为当我得到帖子和作者时,我没有得到时间/日期的任何东西。不管怎样,我只是改变了评论和CreatedAt的顺序,它就成功了!太好了,谢谢!尽管我仍然得到一个错误:注意:试图在第35行的/Applications/MAMP/htdocs/CBP.php中获取非对象的属性。我认为这是“created”属性,因为当我得到帖子和作者时,我没有得到时间/日期的任何东西。不管怎样,我只是改变了评论和CreatedAt的顺序,它就成功了!