简体   繁体   English

从XML获取图像URL

[英]Fetch images URL from XML

I am using the below code to fetch the $movie->id from the response XML 我使用以下代码从响应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;
    }

?> ?>

the response xml structure is 响应xml结构是

在此输入图像描述

How to fetch image URL with thumb size? 如何用拇指大小获取图片网址?

Use the attributes() method of SimpleXmlElement. 使用SimpleXmlElement的attributes()方法。

Example: 例:

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

See the documentation at: http://www.php.net/manual/en/simplexmlelement.attributes.php 请参阅以下文档: 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" : 编辑:仅选择size = "thumb"type = "poster" URL属性:

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

if you expect only 1 url, do: 如果您只想要1个网址,请执行以下操作:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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