我怎样才能从 youtube API 获取此 link 以在我页面的 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 中。下面的脚本将按关键字检索视频列表,但它只会 link 到视频所在的页面,我显然想将它打开到我页面上的 iframe 中。或者至少打开嵌入模式的 youtube。 $watch
变量得到 url 但它返回为 https://www.youtube.com/watch?v=videoID&feature=youtube_gdata_player。即使我手动将它放在那里,它也不会在 iframe 中打开。
有没有办法解析这个。或者 return 完全嵌入而不是 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; ?>
您好,我正在尝试使用 youtube api 获取视频列表,然后将其打开到我网站上的 iframe 中。下面的脚本将按关键字检索视频列表,但它只会 link 到视频所在的页面,我显然想将它打开到我页面上的 iframe 中。或者至少打开嵌入模式的 youtube。 $watch
变量得到 url 但它返回为 https://www.youtube.com/watch?v=videoID&feature=youtube_gdata_player。即使我手动将它放在那里,它也不会在 iframe 中打开。
有没有办法解析这个。或者 return 完全嵌入而不是 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; ?>