Foros del Web » Programando para Internet » PHP »

Leer atributos de un XML

Estas en el tema de Leer atributos de un XML en el foro de PHP en Foros del Web. Hola, estoy trabajando con la API de TheMovieDB y tengo un problema a la hora de leer los atributos del xml que genera. He estado ...
  #1 (permalink)  
Antiguo 12/01/2012, 06:02
 
Fecha de Ingreso: febrero-2011
Ubicación: España
Mensajes: 16
Antigüedad: 13 años, 2 meses
Puntos: 0
Leer atributos de un XML

Hola, estoy trabajando con la API de TheMovieDB y tengo un problema a la hora de leer los atributos del xml que genera. He estado leyendo por el foro y he construido un código pero no funciona.

Este es un ejemplo del xml que quiero leer (http://api.themoviedb.org/2.1/Movie.getImages/en/xml/APIKEY/27205 ):

Código:
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<opensearch:Query searchTerms="27205"/>
<opensearch:totalResults>1</opensearch:totalResults>
<movies>
<movie>
<id>27205</id>
<name>Inception</name>
<images>
<poster id="4ea65cbe34f8633bdc008035">
<image url="http://cf2.imgobject.com/t/p/w92/ziKvu3Th9l1wN2aIeVj5ElpBqFu.jpg" size="thumb" width="92" height="138"/>
<image url="http://cf2.imgobject.com/t/p/w154/ziKvu3Th9l1wN2aIeVj5ElpBqFu.jpg" size="w154" width="154" height="231"/>
<image url="http://cf2.imgobject.com/t/p/w185/ziKvu3Th9l1wN2aIeVj5ElpBqFu.jpg" size="cover" width="185" height="278"/>
<image url="http://cf2.imgobject.com/t/p/w342/ziKvu3Th9l1wN2aIeVj5ElpBqFu.jpg" size="w342" width="342" height="513"/>
<image url="http://cf2.imgobject.com/t/p/w500/ziKvu3Th9l1wN2aIeVj5ElpBqFu.jpg" size="mid" width="500" height="750"/>
<image url="http://cf2.imgobject.com/t/p/original/ziKvu3Th9l1wN2aIeVj5ElpBqFu.jpg" size="original" width="1000" height="1500"/>
</poster>
<poster id="4ea65cbc34f8633bdc008033">
<image url="http://cf2.imgobject.com/t/p/w92/e8MyVAjISnQ8TCcOPQ0Pk2PTe46.jpg" size="thumb" width="92" height="138"/>
<image url="http://cf2.imgobject.com/t/p/w154/e8MyVAjISnQ8TCcOPQ0Pk2PTe46.jpg" size="w154" width="154" height="231"/>
<image url="http://cf2.imgobject.com/t/p/w185/e8MyVAjISnQ8TCcOPQ0Pk2PTe46.jpg" size="cover" width="185" height="278"/>
<image url="http://cf2.imgobject.com/t/p/w342/e8MyVAjISnQ8TCcOPQ0Pk2PTe46.jpg" size="w342" width="342" height="513"/>
<image url="http://cf2.imgobject.com/t/p/w500/e8MyVAjISnQ8TCcOPQ0Pk2PTe46.jpg" size="mid" width="500" height="750"/>
<image url="http://cf2.imgobject.com/t/p/original/e8MyVAjISnQ8TCcOPQ0Pk2PTe46.jpg" size="original" width="1000" height="1500"/>
</poster>
<backdrop id="4ea65c2634f8633bdc007fbd">
<image url="http://cf2.imgobject.com/t/p/w300/cAJOtfT8LvFRMWUXf9dlnpQ2TWf.jpg" size="thumb" width="300" height="188"/>
<image url="http://cf2.imgobject.com/t/p/w780/cAJOtfT8LvFRMWUXf9dlnpQ2TWf.jpg" size="poster" width="780" height="488"/>
<image url="http://cf2.imgobject.com/t/p/w1280/cAJOtfT8LvFRMWUXf9dlnpQ2TWf.jpg" size="w1280" width="1280" height="800"/>
<image url="http://cf2.imgobject.com/t/p/original/cAJOtfT8LvFRMWUXf9dlnpQ2TWf.jpg" size="original" width="1920" height="1200"/>
</backdrop>
</images>
</movie>
</movies>
</OpenSearchDescription>
Y esto es el php que estoy usando para leerlo :

Código PHP:
<?php
$api_title
="http://api.themoviedb.org/2.1/Movie.search/es/xml/APIKEY/".$_GET['titulo'];
$data_title = new SimpleXMLElement($api_titlenulltrue);
foreach(
$data_title->movies->movie as $movie){
    
$id$movie->id;
    
//Primero consigue el Id de la película que estas buscando.
    
echo $id;
    echo 
"<br>";
    
$api_image="http://api.themoviedb.org/2.1/Movie.getImages/es/xml/APIKEY/".$id;
    
//Este es el XML que pongo como ejemplo arriba.
    //Con ese id se conecta para obtener las imágenes.
    
$data_image = new SimpleXMLElement($api_imagenulltrue);
    foreach(
$data_image->movies->movie->images->poster as $poster){
        foreach(
$poster->image as $image){
            foreach(
$image->attributes as $attri){
                
$url $attri->url;
                echo 
$url;
                echo 
"<br>";
            }
        }    
    }    
echo 
"<br>";
}
?>
Lo que quiero es obtener es la URL con size "Original" de cada imagen.
Como puedo hacerlo? MIL GRACIAS
  #2 (permalink)  
Antiguo 12/01/2012, 06:25
Avatar de Eleazan  
Fecha de Ingreso: abril-2008
Ubicación: Ibiza
Mensajes: 1.879
Antigüedad: 16 años
Puntos: 326
Respuesta: Leer atributos de un XML

Código PHP:
foreach($image->attributes as $attri){ 
                
$url $attri->url
                echo 
$url
                echo 
"<br>"
            } 
Debería ser algo así
Código PHP:
foreach($image->attributes() as $attri =>$attri_val){ 
                if(
$attri == 'url') echo $attri_val;
                echo 
"<br>"
            } 
O bien
Código PHP:
echo $images->attributes()->url;
echo 
'<br>'
(Así no recorres toooooodos los attributos q tenga!)

Info: Attributes
__________________
>> Eleazan's Source
>> @Eleazan

Etiquetas: atributos, xml
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:33.