Jump to content

XML Beginner - Getting images with PHP?


Recommended Posts

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.

Edited by RichardHedges
Link to comment
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...