2010-03-20 55 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