0

I am pulling in some youtube video thumbnails into my page with the following:

<img src="http://i3.ytimg.com/vi/<?php echo $youtubelist[$i];?>/default.jpg"></img>

(using a Joomla module to supply the youtube video ID's at $youtubelist)

Along with the youtube thumbnail, I also want to pull in the youtube video 'title' & 'description'.

How do I do this?

Thanks

[edited below]

This is my code & I want to place the title above the tag:

<?php
$youtubelist    = explode( ',', $youtubecode );
$numyoutube     = count($youtubelist);
//Get duplicate module or not
$a=1;
foreach ($list as $item) :
    //$total=$a;
    $enddbid = $item->id;
        if ($ytslide==$enddbid) {$nummod=$a;}
    $a++;   
endforeach;
?>
<div id="videos">
    <div style="padding-left: 2px; padding-right: 2px;padding-bottom:2px;">
    <?php for ($i=0; $i<$numyoutube; $i++) { ?>
        <a href="#">
            <img src="http://i3.ytimg.com/vi/<?php echo $youtubelist[$i];?>/default.jpg"></img>
        </a>
    <?php } ?>
    </div>
</div>
4

2 回答 2

1

您可以使用以下代码获取有关特定 youtube 视频的信息:

$url = "http://gdata.youtube.com/feeds/api/videos/$youtubeid?v=2";
$res = file_get_contents($url);

这将返回带有视频的完整元数据的 Atom XML,包括标题、作者、日期、关键字等。阅读<title>响应的元素以获得您需要的内容。所以这段代码会给你你所追求的:

$data = new DOMDocument();
$res = preg_replace('/>\s+</','><', $res);
$root = $req->loadXML($res);
$tnodes = $root->getElementsByTagName('title');
$tn = $tnodes->item(0);
$title = $tn->firstChild->nodeValue;

使用您的代码,您需要有这样的东西:

<div id="videos">
    <div style="padding-left: 2px; padding-right: 2px;padding-bottom:2px;">
    <?php
        $data = new DOMDocument();
        for ($i=0; $i<$numyoutube; $i++) {
            $url = "http://gdata.youtube.com/feeds/api/videos/" . $youtubelist[$i] . "?v=2";
            $res = file_get_contents($url);
            $res = preg_replace('/>\s+</','><', $res);
            $root = $req->loadXML($res);
            $tnodes = $root->getElementsByTagName('title');
            $tn = $tnodes->item(0);
            $title = $tn->firstChild->nodeValue;
        ?>
        <a href="#">
            <div style="float:left">
                <?php echo $title; ?>
                <br/>
                <img src="http://i3.ytimg.com/vi/<?php echo $youtubelist[$i];?>/default.jpg" />
            </div>
        </a>
    <?php } ?>
    </div>
</div>
于 2012-06-11T06:57:48.697 回答
1

您可以通过在此传递视频 ID 来获取 youtube 视频信息

$video_feed = file_get_contents("http://gdata.youtube.com/feeds/api/videos/$videoid");
                $sxml = new SimpleXmlElement($video_feed);

                //set up nodes
                $namespaces = $sxml->getNameSpaces(true);
                $media = $sxml->children($namespaces['media']);
                $yt = $media->children($namespaces['yt']);
                $yt_attrs = $yt->duration->attributes();

                //vars
                 $video_title = $sxml->title;

                 $video_description = $sxml->content;

                 $video_keywords = $media->group->keywords;

                 $video_length = $yt_attrs['seconds'];
于 2012-06-11T07:00:30.227 回答