SIMPLEXML Help
Will you please help me how can I extract this code with simplexml, this is sample xml feed from flickr
here is a sample xml feed from flickr http://api.flickr.com/services/feeds/photos_public.gne?tags=Ligabue
<link rel="enclosure" type="image/jpeg" href="http://farm3.static.flickr.com/2061/1804769629_383ae90c0c_o.jpg" />
From the code above I want to get the url only which is http://farm3.static.flickr.com/2061/1804769629_383ae90c0c_o.jpg
Thanks in advance...
pr0gr4mm3r posted this at 18:11 — 6th November 2007.
He has: 1,502 posts
Joined: Sep 2006
Like this?
First, load the feed and create the SimpleXML object.
<?php
$xmlstr = file_get_contents('http://api.flickr.com/services/feeds/photos_public.gne?tags=Ligabue');
$xml = new SimpleXMLElement($xmlstr);
?>
Then, figure out where the content is that you want. This means manually sorting through the XML tree (can be annoying) to find the path to the data you want. In your case, the links can be found at $xml->entry[]->link[2]['href']. Since there are multiple entries, we can loop through them like this:
<?php
foreach ($xml->entry as $entry)
?>
Now, inside the loop, you will start at the 'entry' element instead of the root XML element, so to access the link text, use $entry->link[2]['href']. More then one link is provided per entry. The one to the actual photo is the second one. To just output the links from inside the loop to produce the result on this page, use this:
<?php
foreach ($xml->entry as $entry)
{
echo $entry->link[2]['href'] . \"\n\";
}
?>
Put the first and third code snippet together to create the linked result given at the top of the post. Hope this helps.
stephanie schultz posted this at 13:50 — 23rd October 2012.
They have: 1 posts
Joined: Oct 2012
I am kind of a novice at php but there is an error in your last section of code and I don't know how to fix it.
Greg K posted this at 16:28 — 20th January 2013.
He has: 2,145 posts
Joined: Nov 2003
There is an issue with the quotes being escaped.
<?php
foreach ($xml->entry as $entry)
{
echo $entry->link[2]['href'] . "\n"; // output a new line
}
?>
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.