变量未解析 JSON


Variable not parsed JSON

我正在尝试将 Disqus 的 JSON 值解析为 PHP 变量,我能够做到 90%,但由于某种原因,一个特定的变量不起作用。

$comment>消息和$comment>线程在下面的 JSON 中都有值。

JSON:https://disqus.com/api/3.0/forums/listPosts.json?api_key=0B7l7oEVh6xH6EN5BEcEDg4R7tq4RiEmhuLyjnavaUKyOLx23bo099ltdnH9f2p6&forum=greetingtheworld&limit=4

<?php
$endpoint = 'https://disqus.com/api/3.0/forums/listPosts.json?api_key=0B7l7oEVh6xH6EN5BEcEDg4R7tq4RiEmhuLyjnavaUKyOLx23bo099ltdnH9f2p6&forum=greetingtheworld&limit=4';
$j=0;
$cursor=0;
// Standard CURL
$session = curl_init($endpoint.$cursor);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); // instead of just returning true on success, return the result on success
$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;
foreach ($comments as $comment) {
    $name = $comment->author->name; <-- THIS WORKS
    $comment = $comment->message; <-- THIS WORKS
    $thread = $comment->thread; <-- THIS DOESNT WORK
    // Get more data...
    echo '<li class="recentcomments">
        <span class="comment-author-link">';
    echo $name;
    echo '</span> on <a href="2013/10/take-a-deep-breath-and-just-be/index.html#comment-116">';
    echo $comment . $thread;
    echo "</a></li>";
}
?>

执行上述操作时,以下命令工作正常并返回正确的值:

$comment = $comment->message;

但是,下面返回一个错误:

$thread = $comment->thread;

注意:尝试在/home/... 中获取非对象的属性...在第 27 行

非常感谢您的帮助!

在上面的代码中,您存储了$comment->message $comment。因此$comment->thread不起作用,因为您更改了 before 语句中 $comment 的值。因此,您必须将$comment->message存储到任何其他变量,例如 $message .