【问题标题】:Parse XML from YouTube feed (without Zend) [closed]从 YouTube 提要解析 XML(没有 Zend)[关闭]
【发布时间】:2013-03-09 00:23:02
【问题描述】:

我需要从这样的提要中获取非常具体的值:

http://gdata.youtube.com/feeds/api/videos/iIp7OnHXBlo

我必须从这些格式奇怪的 XML 提要中读取的一些节点是:

<title type="text">Uploading YouTube Videos with the PHP Client Library</title>

<media:content url="http://www.youtube.com/v/iIp7OnHXBlo?version=3&f=videos&app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="466" yt:format="5"/>
<media:content url="rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlaBtdxOnuKiBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="466" yt:format="1"/>

<media:description type="plain">Jochen Hartmann demonstrates the basics of how to use the PHP Client Library with the YouTube Data API. [...]</media:description>

<media:thumbnail url="http://i.ytimg.com/vi/iIp7OnHXBlo/0.jpg" height="360" width="480" time="00:03:53"/>
<media:thumbnail url="http://i.ytimg.com/vi/iIp7OnHXBlo/1.jpg" height="90" width="120" time="00:01:56.500"/>
<media:thumbnail url="http://i.ytimg.com/vi/iIp7OnHXBlo/2.jpg" height="90" width="120" time="00:03:53"/>
<media:thumbnail url="http://i.ytimg.com/vi/iIp7OnHXBlo/3.jpg" height="90" width="120" time="00:05:49.500"/>
<yt:duration seconds="466"/>

当然,为 GData 使用 Zend Framework API 绝对不可能(1225 个文件和 49 MB 只是为了解析 XML 文件?是的,当然。 ..我的主。

我需要使用 lastRSS 解析器或任何 PHP 中的内置 XML 函数,就像任何头脑正常的人一样。提前感谢您的任何提示。

【问题讨论】:

标签: php xml parsing youtube xml-namespaces


【解决方案1】:

几周前我遇到了同样的问题。通过添加?format=5&amp;alt=json,您可以获得 JSON 格式的回复,然后您可以轻松地在 PHP 中进行解析

$url = 'http://gdata.youtube.com/feeds/api/videos/iIp7OnHXBlo?format=5&alt=json';
$response = file_get_contents($url);

$obj = json_decode($response);

【讨论】:

    【解决方案2】:

    另一种解决方案是使用这个出色的 xml2Array 类:它基本上会将 xml 转换为超级易于使用的关联数组。

    例如,您发布的 youtube xml 变为:

    Array
    (
        [ENTRY] => Array
        (
            [XMLNS] => http://www.w3.org/2005/Atom
            [XMLNS:MEDIA] => http://search.yahoo.com/mrss/
            [XMLNS:GD] => http://schemas.google.com/g/2005
            [XMLNS:YT] => http://gdata.youtube.com/schemas/2007
            [ID] => Array
                (
    

    等等

    class xml2Array
    {
    
        var $stack=array();
        var $stack_ref;
        var $arrOutput = array();
        var $resParser;
        var $strXmlData;
    
        function push_pos(&$pos)
        {
                $this->stack[count($this->stack)]=&$pos;
                $this->stack_ref=&$pos;
        }
    
        function pop_pos()
        {
                unset($this->stack[count($this->stack)-1]);
                $this->stack_ref=&$this->stack[count($this->stack)-1];
        }
    
        function parse($strInputXML)
        {
                $this->resParser = xml_parser_create ();
                xml_set_object($this->resParser,$this);
                xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
    
                xml_set_character_data_handler($this->resParser, "tagData");
    
                $this->push_pos($this->arrOutput);
    
                $this->strXmlData = xml_parse($this->resParser,$strInputXML );
                if(!$this->strXmlData)
                {
                        die(sprintf("XML error: %s at line %d",
                        xml_error_string(xml_get_error_code($this->resParser)),
                        xml_get_current_line_number($this->resParser)));
                }
    
                xml_parser_free($this->resParser);
    
                return $this->arrOutput;
        }
    
        function tagOpen($parser, $name, $attrs)
        {
                if (isset($this->stack_ref[$name]))
                {
                        if (!isset($this->stack_ref[$name][0]))
                        {
                                $tmp=$this->stack_ref[$name];
                                unset($this->stack_ref[$name]);
                                $this->stack_ref[$name][0]=$tmp;
                        }
                        $cnt=count($this->stack_ref[$name]);
                        $this->stack_ref[$name][$cnt]=array();
                        if (isset($attrs))
                                $this->stack_ref[$name][$cnt]=$attrs;
                        $this->push_pos($this->stack_ref[$name][$cnt]);
                }
                else
                {
                        $this->stack_ref[$name]=array();
                        if (isset($attrs))
                                $this->stack_ref[$name]=$attrs;
                        $this->push_pos($this->stack_ref[$name]);
                }
        }
    
        function tagData($parser, $tagData)
        {
                if(trim($tagData))
                {
                        if(isset($this->stack_ref['DATA']))
                                $this->stack_ref['DATA'] .= $tagData;
                        else
                                $this->stack_ref['DATA'] = $tagData;
                }
        }
    
        function tagClosed($parser, $name)
        {
                $this->pop_pos();
        }
    }
    

    【讨论】:

    • 谢谢!!这非常适合轻松解析文件,效果很好。 :)
    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2021-06-23
    • 2012-01-26
    相关资源
    最近更新 更多