I'm trying to retrieve posts from a Blogger using PHP and XML:
$file="BLOG URL/atom.xml";
$xml = simplexml_load_file($file);
And making a simple loop:
foreach ($xml->entry as $foo) {
echo '<h2>' . $foo->title . '</h2>';
echo '<p>' . $foo->updated . '</p>';
echo $foo->link;
}
The only problem is that link is not showed.
Inspecting the code, each post has more than one link node:
<link href="" rel="replies" title="Postar comentários" type="application/atom+xml"/>
<link href="" rel="replies" title="0 Comentários" type="text/html"/>
<link href="" rel="edit" type="application/atom+xml"/>
<link href="" rel="self" type="application/atom+xml"/>
<link href="" rel="alternate" title="" type="text/html"/>
Is possible to get a node by his type atributte?
You can get this by looping through the links and selecting one. So change:
foreach ($xml->entry as $foo) {
echo '<h2>' . $foo->title . '</h2>';
echo '<p>' . $foo->updated . '</p>';
echo $foo->link;
}
To:
foreach ( $xml->entry as $foo ) {
echo '<h2>' . $foo->title . '</h2>';
echo '<p>' . $foo->updated . '</p>';
foreach ( $foo->link as $link ) {
$type = (string) $link->attributes()->{'type'};
if ( $type == 'text/html' ) {
echo (string) $link->attributes()->{'title'};;
}
}
}
Where 'text/html'
is the type you want to select.