Foros del Web » Programando para Internet » PHP »

simplexml. Extraer datos a variable

Estas en el tema de simplexml. Extraer datos a variable en el foro de PHP en Foros del Web. Hola a todos. Llevo peleándome y buscando varias horas y no encuentro la solución. Intento extraer datos de un feed de una búsqueda de youtube ...
  #1 (permalink)  
Antiguo 03/09/2013, 12:51
Avatar de autonotel  
Fecha de Ingreso: junio-2003
Ubicación: Vigo
Mensajes: 480
Antigüedad: 20 años, 10 meses
Puntos: 13
simplexml. Extraer datos a variable

Hola a todos.

Llevo peleándome y buscando varias horas y no encuentro la solución.

Intento extraer datos de un feed de una búsqueda de youtube a variables php.
Os pongo el feed y luego lo que estoy intentando.

Los datos que intento extraer los pongo en rojo

El feed.
Código:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'
  xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'
  xmlns:batch='http://schemas.google.com/gdata/batch'
  xmlns:yt='http://gdata.youtube.com/schemas/2007'
  xmlns:gd='http://schemas.google.com/g/2005'
  gd:etag='W/"CEEERnk_fCp7ImA9WxRUEU4."'>
  <id>tag:youtube.com,2008:channels</id>
  <updated>2008-11-19T14:10:07.744-08:00</updated>
  <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://gdata.youtube.com/schemas/2007#channel'/>
  <title>YouTube Channels matching query: soccer</title>
  <logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo>
  <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml'
    href='https://gdata.youtube.com/feeds/api/channels?v=2'/>
  <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml'
    href='https://gdata.youtube.com/feeds/api/channels/batch?v=2'/>
  <link rel='self' type='application/atom+xml'
    href='https://gdata.youtube.com/feeds/api/channels?q=soccer&start-index=11&max-results=10&v=2'/>
  <link rel='service' type='application/atomsvc+xml'
    href='https://gdata.youtube.com/feeds/api/channels?alt=atom-service&v=2'/>
  <link rel='next' type='application/atom+xml'
    href='https://gdata.youtube.com/feeds/api/channels?q=soccer&start-index=21&max-results=10&v=2'/>   
  <author>
    <name>YouTube</name>
    <uri>http://www.youtube.com/</uri>
  </author>
  <generator version='2.0' uri='http://gdata.youtube.com/'>YouTube data API</generator>
  <openSearch:totalResults>6141</openSearch:totalResults>
  <openSearch:startIndex>1</openSearch:startIndex>
  <openSearch:itemsPerPage>25</openSearch:itemsPerPage>    
  <entry gd:etag='W/"CEEERnk_fCp7ImA9WxRUEU4."'>
    <id>tag:youtube.com,2008:channel:dXNzb2NjZXJkb3Rjb20</id>
    <updated>2008-11-19T14:10:07.744-08:00</updated>
    <category scheme='http://schemas.google.com/g/2005#kind'
      term='http://gdata.youtube.com/schemas/2007#channel'/>
    <category scheme='http://gdata.youtube.com/schemas/2007/channeltypes.cat'
      term='director'/>
    <title>U.S. Soccer</title>
    <summary>
      ussoccer.com's comprehensive coverage of the U.S. National Teams welcomes
      fans to our YouTube channel - including interviews, press conferences...
    </summary>
    <link rel='http://gdata.youtube.com/schemas/2007#featured-video'
      type='application/atom+xml'
      href='https://gdata.youtube.com/feeds/api/videos/bYKugypF8HA?v=2'/>
    <link rel='alternate' type='text/html'
      href='https://www.youtube.com/profile?user=ussoccerdotcom'/>
    <link rel='self' type='application/atom+xml'
      href='https://gdata.youtube.com/feeds/api/channels/dXNzb2NjZXJkb3Rjb20?v=2'/>
    <author>
      <name>ussoccerdotcom</name>
      <uri>https://gdata.youtube.com/feeds/api/users/ussoccerdotcom</uri>
      <yt:userId>k1pcWQ5E19g0Cgp4c1eI1w</yt:userId>
    </author>
    <yt:channelId>UCk1pcWQ5E19g0Cgp4c1eI1w</yt:userId>
    <yt:channelStatistics subscriberCount='29317' viewCount='1737927'/>
    <gd:feedLink rel='http://gdata.youtube.com/schemas/2007#channel.content'
      href='https://gdata.youtube.com/feeds/api/users/ussoccerdotcom/uploads?v=2'
      countHint='513'/>
    <media:thumbnail url='http://i.ytimg.com/i/k1pcWQ5E19g0Cgp4c1eI1w/1.jpg'/>
  </entry>
  <entry>
    ...
  </entry>
  ...
</feed>
Bueno el "thumbnail" si que me sale.

Ahí va mi php:
Código PHP:
$feedURL "http://gdata.youtube.com/feeds/api/channels?q=$q&v=2";
 
$sxml simplexml_load_file($feedURL);
foreach (
$sxml->entry as $entry) {
//aqui saco la url de la imagen sin problema
$media $entry->children('http://search.yahoo.com/mrss/');
$attributes $media->thumbnail->attributes();
$thumbnail     = (string)$attributes['url'];
//ahora intento las otras dos que me dan resultado en blanco
$autor $entry->autor->name;
$titulo $entry->title
La página no da errores y veo las miniaturas del la búsqueda pero los otros dos datos no aparecen.


Muchas gracias.
  #2 (permalink)  
Antiguo 04/09/2013, 07:59
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: simplexml. Extraer datos a variable

Podrías comenzar por ir depurando tu código, vamos, que de otra forma adivinar está imposible.

Usa var_dump() para examinar cada nodo que vas recorriendo, así te enteras de la estructura que debes leer, etc.
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.

Etiquetas: html, simplexml, variable
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 03:53.