使用 simplexml_load_file() 难以解析原子提要,无法获取属性

发布于 2024-08-26 03:32:56 字数 1591 浏览 10 评论 0 原文

我正在尝试创建一个社交时间表。我从某些地方获取提要,这样我就有了我所做的事情的时间表。我遇到的问题是 Google 阅读器共享项目。

我想获取我共享 中包含的项目的时间尝试使用 $date 获取元素= $xml->entry[$i]->link->attributes()->gr:crawl-timestamp-msec; 失败,因为 gr 后面的 : 会导致 PHP 错误。我可以弄清楚如何获取该元素,所以我想使用下面的代码更改名称,但它会抛出以下错误

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning :无法加载外部实体“<?xml version =“1.0”?>

<?php

$get_feed = file_get_contents('http://www.google.com/reader/public/atom/user/03120403612393553979/state/com.google/broadcast');

    $old = "gr:crawl-timestamp-msec";
    $new  = "timestamp";

    $xml_file = str_replace($old, $new, $get_feed);

    $xml = simplexml_load_file($xml_file);
    $i = 0;


        foreach ($xml->entry as $value)
        { 

            $id = $xml->entry[$i]->id;
            $date = date('Y-m-d H:i:s', strtotime($xml->entry[$i]->attributes()->timestamp ));
            $text = $xml->entry[$i]->title;
            $link = $xml->entry[$i]->link->attributes()->href;
            $source = "googleshared";

            echo "date = $date<br />";

            $sql="INSERT IGNORE INTO timeline (id,date,text,link, source) VALUES ('$id', '$date', '$text', '$link', '$source')";
            mysql_query($sql);

            $i++;
        }`

有人可以给我指出正确的方向吗?

欢呼

克雷格

I am trying to create a social timeline. I pull in feeds form certain places so I have a timeline of thing I have done. The problem I am having is with Google reader Shared Items.

I want to get the time at which I shared the item which is contained in <entry gr:crawl-timestamp-msec="1269088723811"> Trying to get the element using $date = $xml->entry[$i]->link->attributes()->gr:crawl-timestamp-msec; fails because of the : after gr which causes a PHP error. I could figure out how to get the element, so thought I would change the name using the code below but it throws the following error

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "<?xml version="1.0"?><feed xmlns:idx="urn:atom-extension:indexing" xmlns:media="http://search.yahoo.com/mrss/" xmlns

<?php

$get_feed = file_get_contents('http://www.google.com/reader/public/atom/user/03120403612393553979/state/com.google/broadcast');

    $old = "gr:crawl-timestamp-msec";
    $new  = "timestamp";

    $xml_file = str_replace($old, $new, $get_feed);

    $xml = simplexml_load_file($xml_file);
    $i = 0;


        foreach ($xml->entry as $value)
        { 

            $id = $xml->entry[$i]->id;
            $date = date('Y-m-d H:i:s', strtotime($xml->entry[$i]->attributes()->timestamp ));
            $text = $xml->entry[$i]->title;
            $link = $xml->entry[$i]->link->attributes()->href;
            $source = "googleshared";

            echo "date = $date<br />";

            $sql="INSERT IGNORE INTO timeline (id,date,text,link, source) VALUES ('$id', '$date', '$text', '$link', '$source')";
            mysql_query($sql);

            $i++;
        }`

Could someone point me in the right direction please.

Cheers

Craig

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浅浅 2024-09-02 03:32:56

问题是因为 crawl-timestamp-msec 位于不同的命名空间中。在文档中的某个位置(通常是根元素,在您的情况下看起来是 ),它将具有类似于 xmlns:gr="http 的属性://some/url/here”。这表示文档将使用 http://some/url/here 命名空间中的内容,并将为所有这些内容添加前缀 gr

[编辑:有问题的 URL 是 http://www.google.com/schemas/reader/atom/]

要访问它,您需要更改

$xml->entry[$i]->link->attributes()->gr:crawl-timestamp-msec

$xml->entry[$i]->attributes('http://www.google.com/schemas/reader/atom/')->{'crawl-timestamp-msec'}

(编辑:该属性位于 < code> 元素,而不是 ,看起来)

The problem is because crawl-timestamp-msec is in a different namespace. Somewhere in the document (usually the root element, which looks to be <feed/> in your case), it will have an attribute along the lines of xmlns:gr="http://some/url/here". This says that the document will be using things from the http://some/url/here namespace, and will prefix all of these things with gr.

[Edit: the URL in question is http://www.google.com/schemas/reader/atom/]

To access it, you need to change

$xml->entry[$i]->link->attributes()->gr:crawl-timestamp-msec

to

$xml->entry[$i]->attributes('http://www.google.com/schemas/reader/atom/')->{'crawl-timestamp-msec'}

(Edit: the attribute is on the <entry/> element, not the <link/>, it seems)

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