user991830
user991830

Reputation: 894

pull youtube video title into page

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>

Upvotes: 0

Views: 736

Answers (2)

Aleks G
Aleks G

Reputation: 57306

You can get information about a particular youtube video using this piece of code:

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

This will return Atom XML with the full metadata of the video, including title, author, dates, keywords, etc., etc. Read the <title> element of the response to get what you need. So this code will give you what you're after:

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

With your code you'll need to have something like this:

<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>

Upvotes: 1

Pramod Kumar Sharma
Pramod Kumar Sharma

Reputation: 8012

You can fetch the youtube video info by passing the video id in this

$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'];

Upvotes: 1

Related Questions