变量未解析 JSON
Variable not parsed JSON
我正在尝试将 JSON 值从 Disqus 解析为 PHP 变量,我能够做到 90%,但由于某种原因,一个特定的变量不起作用。
$comment->message 和 $comment->thread 都在下面的 JSON 中有值。
<?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>";
}
?>
执行上述操作时,下面的工作正常并且 returns 正确的值:
$comment = $comment->message;
但是下面returns报错:
$thread = $comment->thread;
Notice: Trying to get property of non-object in /home/... on line 27
非常感谢您的帮助!
在上面的代码中,您将 $comment->message
存储到 $comment
。所以 $comment->thread
不起作用,因为您已经在 before 语句中更改了 $comment
的值。
因此您必须将 $comment->message
存储到任何其他变量,例如 $message
。
我正在尝试将 JSON 值从 Disqus 解析为 PHP 变量,我能够做到 90%,但由于某种原因,一个特定的变量不起作用。
$comment->message 和 $comment->thread 都在下面的 JSON 中有值。
<?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>";
}
?>
执行上述操作时,下面的工作正常并且 returns 正确的值:
$comment = $comment->message;
但是下面returns报错:
$thread = $comment->thread;
Notice: Trying to get property of non-object in /home/... on line 27
非常感谢您的帮助!
在上面的代码中,您将 $comment->message
存储到 $comment
。所以 $comment->thread
不起作用,因为您已经在 before 语句中更改了 $comment
的值。
因此您必须将 $comment->message
存储到任何其他变量,例如 $message
。