2017-10-07 87 views
0

我使用下面的代碼。此代碼顯示了我的標題,時間和內容,未顯示我的帖子鏈接。此外,我想顯示最新的更新帖子鏈接,但這不顯示我的鏈接。使用php xml獲取博客最新帖子鏈接

請檢查此代碼。

<?php 
$file="https://*******.blogspot.com/atom.xml"; 
$xml = simplexml_load_file($file); 
foreach ($xml->entry as $foo) { 
    echo '<h2>' . $foo->title . '</h2>'; 
    echo '<p>' . $foo->updated . '</p>'; 
    echo '<p>' . $foo->content . '</p>'; 
    foreach ($foo->link as $link) { 
     $type = (string) $link->attributes()->{'type'}; 
     if ($type == 'text/html') { 
      echo (string) $link->attributes()->{'title'};; 
     } 
    } 
} 
?> 

回答

0

您可以訪問一個元素的屬性,就好像它是一個數組,所以你的代碼看起來應該像...

foreach ($foo->link as $link) { 
    $type = (string) $link['type']; 
    if ($type == 'text/html') { 
     echo (string) $link['title']; 
    } 
} 
+0

沒有顯示帖子鏈接。只顯示標題 –

+0

你的代碼只有標題,如果這是link元素的href,那麼'(string)$ link ['href']'。 –

相關問題