从youtube API中选择内容类型

时间:2011-08-14 20:19:05

标签: php xml youtube simplexml

我有来自youtube API的xml Feed。到目前为止,我已成功取消标题,缩略图,评级等。但是我正在努力解析以下Feed中的内容网址。

<feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/'>
<entry>
<media:group>
<media:content url='http://www.youtube.com/v/ZTUVgYoeN_b?f=gdata_standard...' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='215' yt:format='5'/>

<media:content url='rtsp://rtsp2.youtube.com/ChoLENy73bIAEQ1kgGDA==/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='215' yt:format='1'/>

<media:content url='rtsp://rtsp2.youtube.com/ChoLENy73bIDRQ1kgGDA==/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='215' yt:format='6'/>

</media:group>
</entry>
</feed>

以下是我的代码。我相信这对其他项目有效,因为我没有从多个节点中选择,即只有一个标题,缩略图网址等......

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$xml = curl_exec($ch);
curl_close($ch);

$feed = produce_XML_object_tree($xml);
$entry = $feed->entry;
$media = $entry->children('http://search.yahoo.com/mrss/');
$urla=$media->content->attributes();
$url=$urla["url"];

function produce_XML_object_tree($raw_XML) 
{
libxml_use_internal_errors(true);
try 
    {
    $xmlTree = new SimpleXMLElement($raw_XML);
    } 
    catch (Exception $e) 
    {
    // Something went wrong.
    $error_message = 'SimpleXMLElement threw an exception.';
    foreach(libxml_get_errors() as $error_line) 
        {
        $error_message .= "\t" . $error_line->message;
        }

    trigger_error($error_message);
    return false;
    }
return $xmlTree;
}

如何选择其中一种内容类型?

1 个答案:

答案 0 :(得分:0)

解决

要选择其中一种内容类型,我必须使用简单的索引。希望这是一个我只需要学习一次的课程:

$urla=$media->group->content[0]->attributes();
$url=$urla["url"];