尝试使用 identica-php 显示状态时出现问题
我正在使用 identica-php 使用 获取单个帖子showStatus
,就像这样:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include '../scripts/identica.lib.php';
include '../misc.php';
// your identi.ca username and password
$username = $_GET['u'];
$password = $_GET['p'];
$userid = $_GET['uid'];
$postid = $_GET['pid'];
// initialize the identi.ca class
$identica = new Identica($username, $password, "Terrarium");
// fetch the timeline in xml format
$xml = $identica->showStatus("xml", $postid);
$identica_status = new SimpleXMLElement($xml);
$status = $identica_status->status;
$user = $status->user;
echo '<div id="singleStatus">' . $status->text . "</div><br />";
echo '<div class="single_posted_at">' . $status->created_at . " via " . $status->source . '</div>';
echo '<img src="' . $user->profile_image_url . '" class="identica_image">';
echo '<a href="http://identi.ca/' . $user->screen_name . '" class="nameURL">' . $user->name . '</a>: ';
?>
但是当我尝试运行代码时,我得到的一切都是这样的:
我做错了什么? XML 结果的示例: http://pastebin.com/Q52yfQp9
PS:我已经尝试仅显示 XML 来进行测试,结果成功了,因此帖子 ID 或 XML 不会有问题,但代码会出现问题
I'm using the identica-php to get a single post using showStatus
, just like this:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include '../scripts/identica.lib.php';
include '../misc.php';
// your identi.ca username and password
$username = $_GET['u'];
$password = $_GET['p'];
$userid = $_GET['uid'];
$postid = $_GET['pid'];
// initialize the identi.ca class
$identica = new Identica($username, $password, "Terrarium");
// fetch the timeline in xml format
$xml = $identica->showStatus("xml", $postid);
$identica_status = new SimpleXMLElement($xml);
$status = $identica_status->status;
$user = $status->user;
echo '<div id="singleStatus">' . $status->text . "</div><br />";
echo '<div class="single_posted_at">' . $status->created_at . " via " . $status->source . '</div>';
echo '<img src="' . $user->profile_image_url . '" class="identica_image">';
echo '<a href="http://identi.ca/' . $user->screen_name . '" class="nameURL">' . $user->name . '</a>: ';
?>
But when I try to run the code everything I got is this:
What I'm doing wrong? An example of the XML result: http://pastebin.com/Q52yfQp9
PS: I've tried to show just the XML to do a test and it worked, so it won't be a problem with the Post ID or the XML, but in the code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
status 是 XML 的根元素,因此它在 SimpleXMLElement 对象中没有 getter。
下面重新访问您的代码即可工作:
status is the root element of the XML, so it hasn't a getter in a SimpleXMLElement object.
Below your code revisited to work:
问题不在于 identica-php,而在于您尝试使用 SimpleXMLElement 的方式。您的 $identica_status->user 属性 不是一个数组,它是一个可迭代且可访问的对象(根据 PHP 文档)。
尝试:
或者访问文档树中更下方的元素可能会更简单,如下所示:
The problem isn't identica-php, it's how you're trying to use SimpleXMLElement. Your $identica_status->user property is not an array, it's an iterable and accessible object (according to the PHP docs).
try:
or it might be simpler to just access elements further down in the document tree like this:
您链接到的这个库非常非常旧(09 年 9 月),并且 StatusNet 从那时起已经发展了很多。我对此不再起作用并不感到惊讶。
但是,由于 Identica 的 API 与 Twitter 的 API 类似,因此您可以使用 Twitter PHP 库来完成您想要的操作。
This library you're linking to is really, really old (sept '09) and StatusNet has evolved a lot since then. I'm not surprised this does not work anymore.
However, since Identica's API is similar to Twitter's, you could probably use a Twitter PHP library to do what you want.