2010-03-20 28 views
0

我正在尝试创建社交时间线。我从某些地方吸食饲料,所以我有我做过的事情的时间表。我遇到的问题是Google阅读器共享项目。使用simplexml_load_file()解析原子提要时遇到困难,无法获得属性

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

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++; 
     }` 

有人能指出我在正确的方向吧。

干杯

克雷格

回答

2

问题是因为crawl-timestamp-msec是在不同的命名空间。在文档中的某处(通常是根元素,在您的案例中看起来为<feed/>),它将具有沿着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'}

(编辑:该属性是在<entry/>元素,而不是<link/>,看来)

+0

嗨克里斯, 感谢您的及时回复。知道这是与IBM网站的命名空间相关的东西,但无法弄清楚。我已替换您所说的内容,但会抛出另一个错误 '注意:使用未定义的常量时间戳记 - 假设'时间戳记'在第52行的/Users/craigward/Dropbox/Websites/wip/cron/get_feed_data.php中 :使用未定义的常量毫秒 - 在第52行的/Users/craigward/Dropbox/Websites/wip/cron/get_feed_data.php中假定'msec' date = 0' – 2010-03-20 21:53:42

+1

@Craig使用Chris的答案和我的组合(使用卷曲括号和引号),它应该工作。 – 2010-03-20 22:07:35

+0

和+1 - 我知道还有更多它:) – 2010-03-20 22:08:00