Link to home
Start Free TrialLog in
Avatar of Alex Porter
Alex PorterFlag for United States of America

asked on

PHP SimpleXML finding a specific Attribute

I am using the Flickr API.  The XML below is loaded into an object called $flickrResponse using simplexml_load_string();

I need the source value of the size element where the label is "Thumbnail" (http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg).  I tried the loop below...also tried a for each loop, but it's not working out.  Is there a way to do it without creating a loop?

Thanks in advance for any help with this!
Alex
<rsp stat="ok">
<sizes canblog="1" canprint="1" candownload="1">
<size label="Square" width="75" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_s.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/sq/"/>
<size label="Thumbnail" width="100" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/t/"/>
<size label="Small" width="240" height="180"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_m.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/s/"/>
<size label="Medium" width="500" height="375"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/m/"/>
<size label="Original" width="640" height="480"
      source="http://farm2.static.flickr.com/1103/567229075_6dc09dc6da_o.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/o/"/>
</sizes>
</rsp>
 
	while ($flickrResponse) {
	
		if ($flickrResponse->sizes->size[label] = "Thumbnail") {
		
			echo $flickrResponse->sizes->size[label];
		
			$photoThumb = $flickrResponse->sizes->size[source];
		}
	}

Open in new window

Avatar of Member_2_4694817
Member_2_4694817

If you also have the XML string, then a pattern match helps
$xmlstring = '<rsp stat="ok"><sizes ... </rsp>';
if (preg_match('/<size label="Thumbnail"[^>]+source="([^"]+)"/', $xmlstring, $matches) {
  echo  $matches[1];
} else {
  echo "No thumbnail";
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_2_4694817
Member_2_4694817

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is what I would do.  Object iterators using foreach() are very efficient.  HTH, ~Ray
<?php // RAY_xml_flickr.php
 
// TEST DATA
$xml = '<rsp stat="ok">
<sizes canblog="1" canprint="1" candownload="1">
<size label="Square" width="75" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_s.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/sq/"/>
<size label="Thumbnail" width="100" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/t/"/>
<size label="Small" width="240" height="180"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_m.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/s/"/>
<size label="Medium" width="500" height="375"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/m/"/>
<size label="Original" width="640" height="480"
      source="http://farm2.static.flickr.com/1103/567229075_6dc09dc6da_o.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/o/"/>
</sizes>
</rsp>';
 
// MAKE AN OBJECT FROM THE XML
$obj = SimpleXML_Load_String($xml);
 
// ITERATOR
foreach ($obj->sizes->size as $my_image)
{
 
// NOTE THE USE OF THE ASSOCIATIVE ARRAY INSIDE THE 'size' FIELD
   if ($my_image["label"] == "Thumbnail")
   {
      echo "<br/>{$my_image["source"]}\n";
   }
// END ITERATOR
}

Open in new window

Avatar of Alex Porter

ASKER

This solution was perfect--concise and quick.  Thanks to everyone for the help!