1

我正在使用以下代码从响应 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;
    }

?>

响应xml结构是

在此处输入图像描述

如何获取具有拇指大小的图像 URL?

4

3 回答 3

1

使用 SimpleXmlElement 的 attributes() 方法。

例子:

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

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

于 2013-05-07T11:42:29.727 回答
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/>";
    }
}
于 2013-05-07T11:57:33.410 回答
1

编辑:仅选择带有和URL的属性:size = "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

于 2013-05-07T12:18:32.103 回答