尝试使用 identica-php 显示状态时出现问题

发布于 2024-12-03 18:55:02 字数 1519 浏览 0 评论 0原文

我正在使用 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:
Result of the code

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归属感 2024-12-10 18:55:02

status 是 XML 的根元素,因此它在 SimpleXMLElement 对象中没有 getter。
下面重新访问您的代码即可工作:

//$identica_status = new SimpleXMLElement($xml);
//$status = $identica_status->status;
$status = new SimpleXMLElement($xml);
$user = $status->user;

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_status = new SimpleXMLElement($xml);
//$status = $identica_status->status;
$status = new SimpleXMLElement($xml);
$user = $status->user;
ぽ尐不点ル 2024-12-10 18:55:02

问题不在于 identica-php,而在于您尝试使用 SimpleXMLElement 的方式。您的 $identica_status->user 属性 不是一个数组,它是一个可迭代且可访问的对象(根据 PHP 文档)。

尝试:

$user = $identica_status->user->children();

或者访问文档树中更下方的元素可能会更简单,如下所示:

$identica_status->user->screen_name

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:

$user = $identica_status->user->children();

or it might be simpler to just access elements further down in the document tree like this:

$identica_status->user->screen_name
捎一片雪花 2024-12-10 18:55:02

您链接到的这个库非常非常旧(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文