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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...