Foros del Web » Programando para Internet » PHP »

duda SimpleXML

Estas en el tema de duda SimpleXML en el foro de PHP en Foros del Web. Buenas tengo la siguiente clase para obtener datos de los videos de youtube, pero hay algo que me da error & es en el XML, ...
  #1 (permalink)  
Antiguo 13/09/2012, 20:20
 
Fecha de Ingreso: junio-2009
Mensajes: 138
Antigüedad: 14 años, 10 meses
Puntos: 4
duda SimpleXML

Buenas tengo la siguiente clase para obtener datos de los videos de youtube, pero hay algo que me da error & es en el XML, al momento de obtener las tags del video.

Ignoren el llamado & la asociación con CI, esto no se junta en nada con lo otro & el error no es de CI.

Código PHP:
Ver original
  1. <?php
  2.  
  3. class youtubevid {
  4.    
  5.     var $id;
  6.     var $data;
  7.     var $xml;
  8.    
  9.     private $_ci;
  10.    
  11.     function __construct()
  12.     {
  13.         $this->_ci =& get_instance();
  14.     }
  15.    
  16.     function get_videodata($id)
  17.     {  
  18.         if (strlen($id) >= 22)
  19.         {
  20.             parse_str(parse_url($id, PHP_URL_QUERY), $_id);
  21.             $this->id = $_id['v'];
  22.         }
  23.         else
  24.         {
  25.             $this->id = $id;
  26.         }
  27.        
  28.         $url = 'http://gdata.youtube.com/feeds/videos/' . $this->id;
  29.         $server_output = $this->_yt_curl($url);
  30.  
  31.         if($server_output == 'Invalid id')
  32.         {
  33.             return false;
  34.         }
  35.         else
  36.         {
  37.             $this->data = $server_output;
  38.             $description = $this->prep_desc();
  39.             $this->xml = new SimpleXMLElement($this->data);
  40.             $this->xml->addChild('description', $description);
  41.             return true;
  42.         }
  43.     }
  44.  
  45.     public function is_valid()
  46.     {
  47.         if(empty($this->data))
  48.         {
  49.             return false;
  50.         }
  51.         else
  52.         {
  53.             return true;
  54.         }
  55.     }
  56.  
  57.     public function get_data()
  58.     {
  59.         return $this->data;
  60.     }
  61.  
  62.     public function get_xml()
  63.     {
  64.         return $this->xml;
  65.     }
  66.    
  67.     public function get_title()
  68.     {
  69.         if ($this->is_valid())
  70.         {
  71.             return $this->xml->title;
  72.         }
  73.         else
  74.         {
  75.             return false;
  76.         }
  77.     }
  78.    
  79.     public function get_published()
  80.     {
  81.         if ($this->is_valid())
  82.         {
  83.             return $this->xml->published;
  84.         }
  85.         else
  86.         {
  87.             return false;
  88.         }
  89.     }
  90.    
  91.     public function get_updated()
  92.     {
  93.         if ($this->is_valid())
  94.         {
  95.             return $this->xml->updated;
  96.         }
  97.         else
  98.         {
  99.             return false;
  100.         }
  101.     }
  102.    
  103.     public function get_category()
  104.     {
  105.         if ($this->is_valid())
  106.         {
  107.             $category = '';
  108.             for ($i = 0; $i < count($this->xml->category);$i++)
  109.             {
  110.                 if($this->xml->category[$i]['scheme'] == 'http://gdata.youtube.com/schemas/2007/categories.cat')
  111.                 {
  112.                     $category = $this->xml->category[$i]['label'];
  113.                     break;
  114.                 }
  115.             }
  116.             return $category;
  117.         }
  118.         else
  119.         {
  120.             return false;
  121.         }
  122.     }
  123.    
  124.     public function get_tags()
  125.     {
  126.         if ($this->is_valid())
  127.         {
  128.             $tags = array();
  129.             for ($i = 0; $i < count($this->xml->category);$i++)
  130.             {
  131.                 if ($this->xml->category[$i]['scheme'] == 'http://gdata.youtube.com/schemas/2007/keywords.cat')
  132.                 {
  133.                     $name = $this->xml->category[$i]['term'];
  134.                     array_push($tags, $name);
  135.                 }
  136.             }
  137.             return $tags;
  138.         }
  139.         else
  140.         {
  141.             return false;
  142.         }
  143.     }
  144.    
  145.     public function get_content()
  146.     {
  147.         if ($this->valid())
  148.         {
  149.             return $this->xml->content;
  150.         }
  151.         else
  152.         {
  153.             return false;
  154.         }
  155.     }
  156.  
  157.     public function get_description()
  158.     {
  159.         if($this->is_valid())
  160.         {
  161.             return $this->xml->description;
  162.         }
  163.         else
  164.         {
  165.             return false;
  166.         }
  167.     }
  168.    
  169.     public function get_url()
  170.     {
  171.         if ($this->is_valid())
  172.         {
  173.             return 'http://www.youtube.com/watch?v=' . $this->id;
  174.         }
  175.         else
  176.         {
  177.             return false;
  178.         }
  179.     }
  180.  
  181.     public function get_img_url($option)
  182.     {
  183.         if($this->is_valid())
  184.         {
  185.             if($option == 0)
  186.             {
  187.                 return 'http://i.ytimg.com/vi/'.$this->id.'/0.jpg';
  188.             }
  189.             elseif($option == 1)
  190.             {
  191.                 return 'http://i.ytimg.com/vi/'.$this->id.'/1.jpg';
  192.             }
  193.             elseif($option == 2)
  194.             {
  195.                 return 'http://i.ytimg.com/vi/'.$this->id.'/2.jpg';
  196.             }
  197.             elseif($option == 3)
  198.             {
  199.                 return 'http://i.ytimg.com/vi/'.$this->id.'/3.jpg';
  200.             }
  201.             else
  202.             {
  203.                 return 'http://i.ytimg.com/vi/'.$this->id.'/default.jpg';
  204.             }
  205.         }
  206.         else
  207.         {
  208.             return false;
  209.         }
  210.     }
  211.    
  212.     public function get_author_name()
  213.     {
  214.         if($this->is_valid())
  215.         {
  216.             return $this->xml->author->name;
  217.         }
  218.         else
  219.         {
  220.             return false;
  221.         }
  222.     }
  223.  
  224.     public function get_author_url()
  225.     {
  226.         if ($this->is_valid())
  227.         {
  228.             return 'http://www.youtube.com/user/' . $this->get_author_name();
  229.         }
  230.         else
  231.         {
  232.             return false;
  233.         }
  234.     }
  235.  
  236.     public function get_author_uri()
  237.     {
  238.         if ($this->is_valid())
  239.         {
  240.             return $this->xml->author->uri;
  241.         }
  242.         else
  243.         {
  244.             return false;
  245.         }
  246.     }
  247.    
  248.     public function get_embeb($options = NULL)
  249.     {
  250.         $width = $options['width'];
  251.         if(!isset($options['width']))
  252.         {
  253.             $width = 640;
  254.         }
  255.         unset($options['width']);
  256.  
  257.         $height = $options['height'];
  258.         if (!isset($options['height']))
  259.         {
  260.             $height = 390;
  261.         }
  262.         unset($options['height']);
  263.        
  264.         $secure = '';
  265.         if(isset($options['https']))
  266.         {
  267.             if($options['https'] == 1)
  268.             {
  269.                 $secure = 's';
  270.                 unset($options['https']);
  271.             }
  272.             else
  273.             {
  274.                 $secure = '';
  275.                 unset($options['https']);
  276.             }
  277.         }
  278.  
  279.         if(empty($options))
  280.         {
  281.             $exclamation = '"';
  282.         }
  283.         else
  284.         {
  285.             $exclamation = '?';
  286.         }
  287.        
  288.         $autoplay = $options['autoplay'];
  289.         if(!isset($options['autoplay']))
  290.         {
  291.             $autoplay = 1;
  292.         }
  293.         unset($options['autoplay']);
  294.        
  295.         $embeb_code = '<iframe class="youtube-player" type="text/html" width="' . $width . '" height="' . $height . '" frameborder="0" src="http' . $secure . '://www.youtube.com/embed/' . $this->id /*. $exclamation */ . '?autoplay=' . $autoplay . $exclamation;
  296.  
  297. /*         $i = 1;
  298.         foreach($options as $key => $value)
  299.         {
  300.             if($i == count($options))
  301.             {
  302.                 $embeb_code .= $key.'='.$value.'"';
  303.             }
  304.             else
  305.             {
  306.                 $embeb_code .= $key.'='.$value.'&';
  307.             }
  308.             $i++;
  309.         } */
  310.         $embeb_code .= '></iframe>';
  311.  
  312.         return $embeb_code;
  313.     }
  314.    
  315.     private function _yt_curl($url)
  316.     {
  317.         if($this->_ci->agent->is_browser())
  318.         {
  319.             $agent = $this->_ci->agent->browser().' '.$this->_ci->agent->version();
  320.         }
  321.         elseif($this->_ci->agent->is_robot())
  322.         {
  323.             $agent = $this->_ci->agent->robot();
  324.         }
  325.         elseif($this->_ci->agent->is_mobile())
  326.         {
  327.             $agent = $this->_ci->agent->mobile();
  328.         }
  329.         else
  330.         {
  331.             $agent = 'none';
  332.         }
  333.  
  334.         $curl_handle = curl_init();
  335.         $options = array
  336.         (
  337.             CURLOPT_URL            => $url,
  338.             CURLOPT_HEADER         => false,
  339.             CURLOPT_RETURNTRANSFER => true,
  340.             CURLOPT_USERAGENT      => $agent
  341.         );
  342.         curl_setopt_array($curl_handle, $options);
  343.         $server_output = curl_exec($curl_handle);
  344.         curl_close($curl_handle);
  345.         return $server_output;
  346.     }
  347.    
  348.     private function prep_desc()
  349.     {
  350.         $startstring = "<media:description type='plain'>";
  351.         $endstring = "</media:description>";
  352.  
  353.         $starlocation = strpos($this->data, $startstring);
  354.         $tempstring = substr($this->data, $starlocation);
  355.  
  356.         $endlocation = strpos($tempstring, $endstring);
  357.         $description = substr($tempstring, 0, $endlocation);
  358.  
  359.         if (empty($description))
  360.         {
  361.             $description = FALSE;
  362.         }
  363.         else
  364.         {
  365.             $description = substr($description, strlen($startstring));
  366.         }
  367.  
  368.         return $description;
  369.     }
  370.    
  371. }
  372.  
  373. ?>

Cuando llamo la función get_tags me devuelve solo array's un ejemplo sería este..

Código PHP:
Ver original
  1. (
  2.     [0] => SimpleXMLElement Object
  3.         (
  4.             [0] => donato
  5.         )
  6.  
  7.     [1] => SimpleXMLElement Object
  8.         (
  9.             [0] => estefano
  10.         )
  11.  
  12.     [2] => SimpleXMLElement Object
  13.         (
  14.             [0] => estoy
  15.         )
  16.  
  17.     [3] => SimpleXMLElement Object
  18.         (
  19.             [0] => enamorado
  20.         )
  21.  
  22.     [4] => SimpleXMLElement Object
  23.         (
  24.             [0] => cucho
  25.         )
  26.  
  27.     [5] => SimpleXMLElement Object
  28.         (
  29.             [0] => gamboa
  30.         )
  31.  
  32.     [6] => SimpleXMLElement Object
  33.         (
  34.             [0] => ellos
  35.         )
  36.  
  37.     [7] => SimpleXMLElement Object
  38.         (
  39.             [0] => cover
  40.         )
  41.  
  42.     [8] => SimpleXMLElement Object
  43.         (
  44.             [0] => cuchogol
  45.         )
  46.  
  47. )

Pero quiero me muestre solo las tags del video & que se muestren separadas por comas.. como lo puedo hacer?

De antemano, gracias.
  #2 (permalink)  
Antiguo 14/09/2012, 00:23
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: duda SimpleXML

Código PHP:
Ver original
  1. // En esta parte
  2.                 if ($this->xml->category[$i]['scheme'] == 'http://gdata.youtube.com/schemas/2007/keywords.cat')
  3.                 {
  4.                     $name = $this->xml->category[$i]['term'];
  5.  
  6.                     // Verifica lo que tienes en $name
  7.                     var_dump($name);
  8.                     // Esto te debe dar una pista para solucionarlo
  9.  
  10.                     array_push($tags, $name);
  11.                 }
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 14/09/2012, 05:18
 
Fecha de Ingreso: junio-2009
Mensajes: 138
Antigüedad: 14 años, 10 meses
Puntos: 4
Respuesta: duda SimpleXML

La verdad queda igual. Pero no entiendo como hacer para que solo me muestre las tags..

Código PHP:
Ver original
  1. object(SimpleXMLElement)#36 (1) {
  2.  [0]=>
  3.   string(6) "donato"
  4. }
  5. object(SimpleXMLElement)#38 (1) {
  6.  [0]=>
  7.   string(8) "estefano"
  8. }
  9. object(SimpleXMLElement)#39 (1) {
  10.  [0]=>
  11.   string(5) "estoy"
  12. }
  13. object(SimpleXMLElement)#40 (1) {
  14.  [0]=>
  15.   string(9) "enamorado"
  16. }
  17. object(SimpleXMLElement)#41 (1) {
  18.  [0]=>
  19.   string(5) "cucho"
  20. }
  21. object(SimpleXMLElement)#42 (1) {
  22.  [0]=>
  23.   string(6) "gamboa"
  24. }
  25. object(SimpleXMLElement)#43 (1) {
  26.  [0]=>
  27.   string(5) "ellos"
  28. }
  29. object(SimpleXMLElement)#44 (1) {
  30.  [0]=>
  31.   string(5) "cover"
  32. }
  33. object(SimpleXMLElement)#45 (1) {
  34.  [0]=>
  35.   string(8) "cuchogol"
  36. }
  37. (
  38.     [0] => SimpleXMLElement Object
  39.         (
  40.             [0] => donato
  41.         )
  42.  
  43.     [1] => SimpleXMLElement Object
  44.         (
  45.             [0] => estefano
  46.         )
  47.  
  48.     [2] => SimpleXMLElement Object
  49.         (
  50.             [0] => estoy
  51.         )
  52.  
  53.     [3] => SimpleXMLElement Object
  54.         (
  55.             [0] => enamorado
  56.         )
  57.  
  58.     [4] => SimpleXMLElement Object
  59.         (
  60.             [0] => cucho
  61.         )
  62.  
  63.     [5] => SimpleXMLElement Object
  64.         (
  65.             [0] => gamboa
  66.         )
  67.  
  68.     [6] => SimpleXMLElement Object
  69.         (
  70.             [0] => ellos
  71.         )
  72.  
  73.     [7] => SimpleXMLElement Object
  74.         (
  75.             [0] => cover
  76.         )
  77.  
  78.     [8] => SimpleXMLElement Object
  79.         (
  80.             [0] => cuchogol
  81.         )
  82.  
  83. )
  #4 (permalink)  
Antiguo 14/09/2012, 09:29
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: duda SimpleXML

Código PHP:
Ver original
  1. // En esta parte
  2. * * * * * * * * if ($this->xml->category[$i]['scheme'] == 'http://gdata.youtube.com/schemas/2007/keywords.cat')
  3. * * * * * * * * {
  4. * * * * * * * * * * $name = (string) $this->xml->category[$i]['term'];
  5. *
  6. * * * * * * * * * * array_push($tags, $name);
  7. * * * * * * * * }

Luego puedes usar implode() para pasarlos a una lista separada por comas.

Saludos.
  #5 (permalink)  
Antiguo 14/09/2012, 14:12
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: duda SimpleXML

Rsulta que $name es un objeto que sólo contiene una cadena, y tú lo estás tratando directamente como si fuera cadena.

Prueba haciendo el cast a string, como sugiere GatorV.
__________________
- León, Guanajuato
- GV-Foto
  #6 (permalink)  
Antiguo 14/09/2012, 17:58
 
Fecha de Ingreso: junio-2009
Mensajes: 138
Antigüedad: 14 años, 10 meses
Puntos: 4
Respuesta: duda SimpleXML

Ahora si que cambia :) Pero solo me queda una duda, no conozco mucho como tratar los arrays & me devuelve esto:

Código PHP:
Ver original
  1. (
  2.     [0] => donato
  3.     [1] => estefano
  4.     [2] => estoy
  5.     [3] => enamorado
  6.     [4] => cucho
  7.     [5] => gamboa
  8.     [6] => ellos
  9.     [7] => cover
  10.     [8] => cuchogol
  11. )

& lo solucione así:

Código PHP:
Ver original
  1. $cont = 0;
  2. foreach($tags as $tag)
  3. {
  4.     echo $tag . '<br/>';
  5.     $cont++;
  6. }

Como puedo agregar lo del implode() para que se muestren separadas por comas?

Última edición por Lokitozzz; 14/09/2012 a las 18:07
  #7 (permalink)  
Antiguo 14/09/2012, 18:10
 
Fecha de Ingreso: junio-2009
Mensajes: 138
Antigüedad: 14 años, 10 meses
Puntos: 4
Respuesta: duda SimpleXML

Ya hice lo del implode, graciassss :)))))

Etiquetas: html, simplexml
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 11:01.