从XML获取图像URL

时间:2013-05-07 11:36:45

标签: php xml simplexml

我使用以下代码从响应XML

中获取$ movie-> id
<?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;
    }

&GT;

响应xml结构

enter image description here

如何使用拇指大小获取图片网址?

3 个答案:

答案 0 :(得分:1)

使用SimpleXmlElement的attributes()方法。

示例:

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

请参阅以下文档:http://www.php.net/manual/en/simplexmlelement.attributes.php

答案 1 :(得分:1)

请参阅下面的简单方法,仅获取特定图像。

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

答案 2 :(得分:1)

编辑:仅使用URLsize = "thumb"选择type = "poster"个属性:

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

如果您只想要1个网址,请执行:

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

现场演示演示:http://codepad.viper-7.com/wdmEay