du512926 2012-06-11 06:49
浏览 21
已采纳

将youtube视频标题拉入页面

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>
  • 写回答

2条回答 默认 最新

  • dounao2829 2012-06-11 06:57
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。