簡體   English   中英

從XML獲取圖像URL

[英]Fetch images URL from 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;
    }

?>

響應xml結構是

在此輸入圖像描述

如何用拇指大小獲取圖片網址?

使用SimpleXmlElement的attributes()方法。

例:

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

請參閱以下文檔: http//www.php.net/manual/en/simplexmlelement.attributes.php

請參閱下面一個簡單的方法來獲取特定圖像。

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

編輯:僅選擇size = "thumb"type = "poster" URL屬性:

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM