用php在xml中提取youtube

时间:2014-01-27 16:53:30

标签: php xml youtube

我正在学习使用phph的XML,而我正在尝试使用youtube的视频链接

我已经设法获得描述每个视频的exatract的标题但是我无法获得视频链接我的问题源于每个条目有几个链接

 <entry>
        <id>http://gdata.youtube.com/feeds/api/videos/yN38y5MILbs</id>
        <published>2013-04-26T16:00:21.000Z</published>
        <updated>2014-01-24T19:24:02.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="Comedy" label="Comedy" />
        <title type="text">SURICATE - Movies vs. Life</title>
        <content type="text">Si tout pouvait se passer comme dans les films... Ce serait bien agréable. Si toi aussi tu es fan de ciné, prouve-le et gagne ta place pour MyWarnerDay sur h...</content>
        <link rel="alternate" type="text/html" href="https://www.youtube.com/watch?v=yN38y5MILbs&amp;feature=youtube_gdata" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.ratings" type="application/atom+xml" href="https://gdata.youtube.com/feeds/api/videos/yN38y5MILbs/ratings" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.complaints" type="application/atom+xml" href="https://gdata.youtube.com/feeds/api/videos/yN38y5MILbs/complaints" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="https://gdata.youtube.com/feeds/api/videos/yN38y5MILbs/related" />
        <link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="https://m.youtube.com/details?v=yN38y5MILbs" />
        <link rel="self" type="application/atom+xml" href="https://gdata.youtube.com/feeds/api/videos/yN38y5MILbs" />
        <author>
            <name>GoldenMoustacheVideo</name>
            <uri>https://gdata.youtube.com/feeds/api/users/GoldenMoustacheVideo</uri>
        </author>
        <gd:comments>
            <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="https://gdata.youtube.com/feeds/api/videos/yN38y5MILbs/comments" countHint="3002" />
        </gd:comments>
        <yt:hd />
        <media:group>
            <media:category label="Comedy" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Comedy</media:category>
            <media:content url="https://www.youtube.com/v/yN38y5MILbs?version=3&amp;f=videos&amp;d=AdCwUSS4vp5b8jXYalmwHVYO88HsQjpE1a8d1GxQnGDm&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="159" yt:format="5" />
            <media:content url="rtsp://r5---sn-p5qlsu7z.c.youtube.com/CkULENy73wIaPAm7LQiTy_zdyBMYDSANFEgGUgZ2aWRlb3NyIQHQsFEkuL6eW_I12GpZsB1WDvPB7EI6RNWvHdRsUJxg5gw=/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="159" yt:format="1" />
            <media:content url="rtsp://r5---sn-p5qlsu7z.c.youtube.com/CkULENy73wIaPAm7LQiTy_zdyBMYESARFEgGUgZ2aWRlb3NyIQHQsFEkuL6eW_I12GpZsB1WDvPB7EI6RNWvHdRsUJxg5gw=/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="159" yt:format="6" />
            <media:description type="plain">Si tout pouvait se passer comme dans les films... Ce serait bien agréable. Si toi aussi tu es fan de ciné, prouve-le et gagne ta place pour MyWarnerDay sur h...</media:description>
            <media:keywords />
            <media:player url="https://www.youtube.com/watch?v=yN38y5MILbs&amp;feature=youtube_gdata_player" />
            <media:thumbnail url="https://i.ytimg.com/vi/yN38y5MILbs/0.jpg" height="360" width="480" time="00:01:19.500" />
            <media:thumbnail url="https://i.ytimg.com/vi/yN38y5MILbs/1.jpg" height="90" width="120" time="00:00:39.750" />
            <media:thumbnail url="https://i.ytimg.com/vi/yN38y5MILbs/2.jpg" height="90" width="120" time="00:01:19.500" />
            <media:thumbnail url="https://i.ytimg.com/vi/yN38y5MILbs/3.jpg" height="90" width="120" time="00:01:59.250" />
            <media:title type="plain">SURICATE - Movies vs. Life</media:title>
            <yt:duration seconds="159" />
        </media:group>
        <gd:rating average="4.9594884" max="5" min="1" numRaters="58156" rel="http://schemas.google.com/g/2005#overall" />
        <yt:statistics favoriteCount="0" viewCount="4916249" />
    </entry>

我在php中所做的是这个很好但不是链接我可以有一些指示

foreach ($video->entry as $videoinfo){

  echo '<h2>'.$videoinfo->title.'</h2><br>';
  echo  '<p>'.$videoinfo->content.'<br>';
  echo $videoinfo->link.'<br></p>';
  }

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

如果你深入挖掘xml,你可以找到链接(你仍然可以通过这种方式获得标题和描述)。

foreach ($video -> entry as $videoinfo){
    $mediaElem = $videoinfo -> children( 'media', true );

    echo '<h2>' . $mediaElem -> group -> title . '</h2>';
    echo '<p'> . $mediaElem -> group -> description . '<br>';
    echo "<a href='" . $mediaElem -> group -> player -> attributes() -> url . "'>Watch on YouTube</a></p>"
}