0

我从 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 为空。如何得到它?

4

1 回答 1

0

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

尝试使用:

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

尝试在此处阅读更多相关信息:http: //www.w3schools.com/xml/xml_namespaces.aspPHP 库,用于在标记名称中使用冒号解析 XML?

另请注意,秒是持续时间标签的属性,您试图将其作为值访问。

于 2013-07-17T09:16:15.400 回答