RichardHedges Posted March 22, 2011 Share Posted March 22, 2011 Hello everyone,I'm new to XML but it's vital for a script I'm making.I'm trying to retrieve XML from a document with PHP and have managed up until now.The XML I've got is: <OpenSearchDescription> <opensearch:Query searchTerms="6479"/> <opensearch:totalResults>1</opensearch:totalResults> <movies> <movie> <name>I Am Legend</name> <images> <image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-mid.jpg" size="mid" width="500" height="741" id="4bc91de5017a3c57fe00bb7a"/> <image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-cover.jpg" size="cover" width="185" height="274" id="4bc91de5017a3c57fe00bb7a"/> <image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-thumb.jpg" size="thumb" width="92" height="136" id="4bc91de5017a3c57fe00bb7a"/> </images> </movie> </movies></OpenSearchDescription> The PHP code I currently have to call the name is as follows: $doc = new DOMDocument();$url = 'http://api.themoviedb.org/2.1/Movie.getInfo/en/xml/'.$movie_api_key.'/'.$movieid.'';$doc->load($url); $movies = $doc->getElementsByTagName("movie");$c = 0;foreach ($movies as $movie) { $c++; $names = $movie->getElementsByTagName("name"); $name = $names->item(0)->nodeValue; if ($c > 1) { break; } echo $name;} So that displays the movie name, however the images section confused me. Because there's more than one item with <images> I've no idea how to call the image with size (size="cover") cover.I'm sorry this is such a beginner question, but I'm really stumped.Can anyone provide me with some PHP code to retrieve the image?(I'm sorry if this is in the wrong place. I wasn't sure whether it should be in PHP or XML and I didn't want to post it twice.)Thanks. Link to comment Share on other sites More sharing options...
Ingolme Posted March 23, 2011 Share Posted March 23, 2011 You have to look through each image element and check the size attribute. If the size attribute is "cover" then break from the loop and use the value of that element.Use foreach() to loop through all the <image> elements within the <images> element. Link to comment Share on other sites More sharing options...
RichardHedges Posted March 24, 2011 Author Share Posted March 24, 2011 Thank you so much for explaining it! It's so much better than just giving me the code as I'll learn this way.Thanks Ingolme! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.