简体   繁体   中英

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

在此输入图像描述

How to fetch image URL with thumb size?

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

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/>";
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM