使用php xml获取博客最新帖子链接
问题描述:
我使用下面的代码。此代码显示了我的标题,时间和内容,未显示我的帖子链接。此外,我想显示最新的更新帖子链接,但这不显示我的链接。使用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'};;
}
}
}
?>
答
您可以访问一个元素的属性,就好像它是一个数组,所以你的代码看起来应该像...
foreach ($foo->link as $link) {
$type = (string) $link['type'];
if ($type == 'text/html') {
echo (string) $link['title'];
}
}
没有显示帖子链接。只显示标题 –
你的代码只有标题,如果这是link元素的href,那么'(string)$ link ['href']'。 –