Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/09/2012, 20:20
Lokitozzz
 
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.