Yogus
Yogus

Reputation: 2272

Fetch images URL from XML

I am using the below code to fetch the $movie->id from the response XML

<?php   
$movie_name='Dabangg 2';
        $url ='http://api.themoviedb.org/2.1/Movie.search/en/xml/accd3ddbbae37c0315fb5c8e19b815a5/%22Dabangg%202%22';

    $xml = simplexml_load_file($url);
    $movies = $xml->movies->movie;
   foreach ($movies as $movie){
        $arrMovie_id = $movie->id;
    }

?>

the response xml structure is

enter image description here

How to fetch image URL with thumb size?

Upvotes: 1

Views: 1451

Answers (3)

michi
michi

Reputation: 6625

EDIT: select only URL attributes with size = "thumb" and type = "poster":

$urls = $xml->xpath("//image[@size='thumb' and @type='poster']/@url");

if you expect only 1 url, do:

$url = (string)$xml->xpath("//image[@size='thumb' and @type='poster']/@url")[0];
echo $url;

working live demo: http://codepad.viper-7.com/wdmEay

Upvotes: 1

Smile
Smile

Reputation: 2758

See the below an easy way to get only specific images.

$xml = simplexml_load_file($url);
$images = $xml->xpath("//image");
//echo "<pre>";print_r($images);die;
foreach ($images as $image){
    if($image['size'] == "thumb"){      
        echo "URL:".$image['url']."<br/>";
        echo "SIZE:".$image['size']."<br/>";
        echo "<hr/>";
    }
}

Upvotes: 1

Damien Overeem
Damien Overeem

Reputation: 4529

Use the attributes() method of SimpleXmlElement.

Example:

$imageAttributes = $movie->images[0]->attributes();
$size = $imageAttributes['size'];

See the documentation at: http://www.php.net/manual/en/simplexmlelement.attributes.php

Upvotes: 1

Related Questions