我如何从youtube API获得这个链接,以便在我的页面中的Iframe中打开,或者至少在模态中打开


How can I go about getting this link from the youtube API to open in an Iframe in my page or at least open in a modal

嗨,我正在尝试使用youtube api获取视频列表,然后将其打开到我网站上的iframe中。下面的脚本将按关键字检索视频列表,但它只会链接到视频所在的页面,我显然想将其打开到我页面上的iframe中。或者至少打开一个模态嵌入的youtube。$watch变量获取url,但返回为https://www.youtube.com/watch?v=videoID&feature=youtube_gdata_player。即使我手动将它放在iframe中,它也不会在那里打开。

有没有办法解析这个。或者使用完全嵌入而不是watch=tv返回它。我也不确定是否有办法在创建的<a>标记中回显target="video-frame"。我需要使用另一个变量来获取视频url吗?当我这样做时,服务器拒绝页面时,出于某种原因,我将如何将其写为<a href="" target="video-frame"></a>

<?php
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/-/Keyword/';
$sxml = simplexml_load_file($feedURL);
$counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
$total = $counts->totalResults;  
?>
<?php    
foreach ($sxml->entry as $entry) {
  $media = $entry->children('http://search.yahoo.com/mrss/');
  $attrs = $media->group->player->attributes();
  $watch = $attrs['url']; 
  $yt = $media->children('http://gdata.youtube.com/schemas/2007');
  $attrs = $yt->duration->attributes();
  $length = $attrs['seconds']; 
  $gd = $entry->children('http://schemas.google.com/g/2005'); 
  if ($gd->rating) {
    $attrs = $gd->rating->attributes();
    $rating = $attrs['average']; 
  } else {
    $rating = 0; 
  } 
  echo "<li>'n";
  echo "<a href='"{$watch}'">{$media->group->title}</a>
  <br/>'n";
  echo sprintf("%0.2f", $length/60) . " min. | {$rating} user rating
  <br/>'n";
  echo "{$media->group->description}<p/>'n";
  echo "<p/></li>'n";
}
?>

您可以获取VideoID,然后将其放入iframe中。这是代码:

<?php
$string = "https://www.youtube.com/watch?v=videoID&feature=youtube_gdata_player";
$startpoint=strpos($string,'watch?v=');$startpoint=$startpoint+8;
$length=strpos($string,'&');$length = $length - $startpoint;
$videoId =  substr($string,$startpoint,$length);
$ytstring = '<iframe width="560" height="315" src="https://www.youtube.com/embed/'.$videoId.'" frameborder="0" allowfullscreen></iframe>';
echo $videoId;
echo $ytstring;
?> 

首先,我建议使用JSON而不是XML提要。为了完成将视频加载到Iframe中,您需要使用Javascript。

这里有一个可以参考的快速演示:

 <?php 
//YouTube json feed example:
$keyword = "basketball";
$file = file_get_contents("https://gdata.youtube.com/feeds/api/videos/-/$keyword?v=2&alt=json");
//decode into array
$decoded = json_decode($file, true);
//point the feed to 'entry' array
$entries = $decoded['feed']['entry'];
//parse through entries
if (!empty($entries)) {
    for($i=0; $i<count($entries); $i++) {
        $thumb = $entries[$i]['media$group']['media$thumbnail'][0]['url'];
        $title = $entries[$i]['title']['$t'];
        $description = $entries[$i]['media$group']['media$description']['$t'];
        $video = "https://www.youtube.com/embed/".$entries[$i]['media$group']['yt$videoid']['$t']."?wmode=opaque";
        $published = date('Y-m-d H:i:s', strtotime($entries[$i]['published']['$t']));
        $content[]= "<a href='"javascript:yt_load_video('$video');'"><img src='"$thumb'"><br />$title</a><br /><small>$description<hr />$published</small>";
    }           
}
?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
function yt_load_video(url) {
    $("iframe").attr("src",url);
    return;
}
</script>
<?php if (!empty($content)) : ?>
    <iframe type="text/html" src="https://www.youtube.com/embed/<?php echo $entries[0]['media$group']['yt$videoid']['$t']; ?>?wmode=opaque" frameborder="0" width="400" height="300"></iframe>
    <hr />
    <?php foreach($content as $thumb) : ?>
        <div style="float:left; margin:4px; width:200px; height:200px; overflow:scroll"><?php echo $thumb ?></div>
    <?php endforeach; ?>
<?php endif; ?>