0

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

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 函数,就像任何头脑正常的人一样。提前感谢您的任何提示。

4

2 回答 2

6

几周前我遇到了同样的问题。通过添加?format=5&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);
于 2013-03-20T03:08:18.690 回答
0

另一个解决方案是使用这个优秀的 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();
    }
}
于 2013-03-20T03:28:17.967 回答