如何使用simpeXML从youtube api获取视频时长



我从youtube 收到了这个xml响应

<?xml version="1.0" encoding="UTF-8"?>
<entry 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>http://gdata.youtube.com/feeds/api/videos/9mZb6PZM4FU</id>
   <published>2010-01-09T19:38:15.000Z</published>
   <updated>2013-07-17T06:51:32.000Z</updated>
   <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" />
   <category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="Entertainment" label="Розваги" />
   <title type="text">Каратэ</title>
   <content type="text">Сцена из фильма "Не бойся, я с тобой"</content>
   <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=9mZb6PZM4FU&amp;feature=youtube_gdata" />
   <link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/9mZb6PZM4FU/responses" />
   <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/9mZb6PZM4FU/related" />
   <link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=9mZb6PZM4FU" />
   <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/9mZb6PZM4FU" />
   <author>
      <name>Lefan123</name>
      <uri>http://gdata.youtube.com/feeds/api/users/Lefan123</uri>
   </author>
   <gd:comments>
      <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="http://gdata.youtube.com/feeds/api/videos/9mZb6PZM4FU/comments" countHint="80" />
   </gd:comments>
   <media:group>
      <media:category label="Розваги" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Entertainment</media:category>
      <media:content url="http://www.youtube.com/v/9mZb6PZM4FU?version=3&amp;f=videos&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="174" yt:format="5" />
      <media:content url="rtsp://r4---sn-4g57kuek.c.youtube.com/CiILENy73wIaGQlV4Ez26Ftm9hMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="174" yt:format="1" />
      <media:content url="rtsp://r4---sn-4g57kuek.c.youtube.com/CiILENy73wIaGQlV4Ez26Ftm9hMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="174" yt:format="6" />
      <media:description type="plain">Сцена из фильма "Не бойся, я с тобой"</media:description>
      <media:keywords />
      <media:player url="http://www.youtube.com/watch?v=9mZb6PZM4FU&amp;feature=youtube_gdata_player" />
      <media:thumbnail url="http://i1.ytimg.com/vi/9mZb6PZM4FU/0.jpg" height="360" width="480" time="00:01:27" />
      <media:thumbnail url="http://i1.ytimg.com/vi/9mZb6PZM4FU/1.jpg" height="90" width="120" time="00:00:43.500" />
      <media:thumbnail url="http://i1.ytimg.com/vi/9mZb6PZM4FU/2.jpg" height="90" width="120" time="00:01:27" />
      <media:thumbnail url="http://i1.ytimg.com/vi/9mZb6PZM4FU/3.jpg" height="90" width="120" time="00:02:10.500" />
      <media:title type="plain">Каратэ</media:title>
      <yt:duration seconds="174" />
   </media:group>
   <gd:rating average="4.934732" max="5" min="1" numRaters="429" rel="http://schemas.google.com/g/2005#overall" />
   <yt:statistics favoriteCount="0" viewCount="215169" />
</entry>

我试图通过使用这个代码获得持续时间

public function getVideoLength($id)
    {
        $this->getVideoFeed($id);
        if ($this->xmlEntry == null) return 0;
        $duration = $this->xmlEntry->media['group']->yt['duration'];        
        return $duration;
    }

但是$duration为null。如何获取?

xml中的冒号用于定义不同的名称空间。您需要将命名空间切换到媒体才能访问持续时间。

尝试使用:

$this->xmlEntry->children('media', true)->group->children('yt', true)->duration->attributes()["seconds'];

请尝试在此处阅读更多信息:http://www.w3schools.com/xml/xml_namespaces.asp以及用于解析标记名中带有冒号的XML的PHP库?

还要注意,秒是duration标记的一个属性,您试图将其作为值进行访问。

相关内容

  • 没有找到相关文章

最新更新