1

I'm trying to use an API with the following XML:

<movies>
   <movie>
      <images>
         <image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-original.jpg" size="original" width="675" height="1000" id="4bc91de5017a3c57fe00bb7a"/>
         <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"/>
      </images>
   </movie>
</movies>

Can someone give me an example of the PHP code I should use to get the image url where size="cover"?

Thanks.

1
  • Have you tried anything yet? Look at some PHP XML parser tutorials which will help you get started. This will help you learn what you do rather than just copying someones code. Commented Mar 24, 2011 at 23:23

4 Answers 4

2

SimpleXML can do this quite, for lack of a better word, simply:

$xml = new SimpleXMLElement($str);
$xpath = $xml->xpath("/movies/movie/images/image[@size = 'cover']");

echo $xpath[0]['url'];
Sign up to request clarification or add additional context in comments.

1 Comment

Caveat: simplexml loads the whole XML tree in memory at once. So it only works with relatively small XML files. For larger XML files use a stream-based reader such as XMLReader.
0

Load the xml with a XML Parser, DOMDocument, SimpleXML etc.

http://se.php.net/manual/en/refs.xml.php

Then you can use XPath to select the image. http://www.w3schools.com/xpath/xpath_syntax.asp

XPath to grab movie with attribute size = cover

/movies/movie/images/image[@size=cover]

Looks like a good tutorial: http://ditio.net/2008/12/01/php-xpath-tutorial-advanced-xml-part-1/

Comments

0
<?php
$string = <<<XML
<?xml version='1.0'?>
<movies>
  <movie>
     <images>
        <image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-original.jpg" size="original" width="675" height="1000" id="4bc91de5017a3c57fe00bb7a"/>
        <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"/>
     </images>
  </movie>
</movies>
XML;

$xml = simplexml_load_string($string);

foreach($xml->movie->images->image as $image) {

    if(strcmp($image['size'],"cover") == 0)
        echo $image['url'];
}

?>

Comments

0

$xml = simplexml_load_string($string2); foreach($xml->movie->images->image as $image) {

if(strcmp($image['size'],"cover"))
   // echo $image['url'];
    ?>

" width="200px" height="100px">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.