使用 PHP 从 Youtube 视频 URL 获取 Youtube 视频缩略图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6808013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the Youtube Video Thumbnail from Youtube video Url using PHP
提问by Neo
Lets say I have a youtube video url www.youtube.com/watch?v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2
假设我有一个 youtube 视频网址 www.youtube.com/watch?v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2
I want to get the video thumbnail -> i3.ytimg.com/vi/B4CRkpBGQzU/default.jpg I just need a simple php script that I will input the $url and get the $img If posible I would like to strip all other parameters and be left with just the www.youtube.com/watch?v=B4CRkpBGQzU as the $url
我想获取视频缩略图 -> i3.ytimg.com/vi/B4CRkpBGQzU/default.jpg 我只需要一个简单的 php 脚本,我将输入 $url 并获取 $img 如果可能,我想删除所有其他参数并只留下 www.youtube.com/watch?v=B4CRkpBGQzU 作为 $url
回答by Pascal MARTIN
To extract the identifier from the following URL :
从以下 URL 中提取标识符:
$url = 'www.youtube.com/watch?v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2';
You can first use parse_url()
to get the query string :
您可以首先使用parse_url()
来获取查询字符串:
$queryString = parse_url($url, PHP_URL_QUERY);
var_dump($queryString);
Which, here, would give you :
在这里,会给你:
string 'v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2' (length=49)
And, then, use parse_str()
to extract the parameters from that query string :
然后,用于parse_str()
从该查询字符串中提取参数:
parse_str($queryString, $params);
var_dump($params);
Which would get you the following array :
这会让你得到以下数组:
array
'v' => string 'B4CRkpBGQzU' (length=11)
'feature' => string 'youtube_gdata' (length=13)
'par1' => string '1' (length=1)
'par2' => string '2' (length=1)
And, now, it's just a matter of using the v
item from that array, injecting it into the thumbnail URL :
而且,现在,只需使用该v
数组中的项目,将其注入缩略图 URL 即可:
if (isset($params['v'])) {
echo "i3.ytimg.com/vi/{$params['v']}/default.jpg";
}
Which gives :
这使 :
i3.ytimg.com/vi/B4CRkpBGQzU/default.jpg
回答by Jerry Jones
<?php
function getYoutubeImage($e){
//GET THE URL
$url = $e;
$queryString = parse_url($url, PHP_URL_QUERY);
parse_str($queryString, $params);
$v = $params['v'];
//DISPLAY THE IMAGE
if(strlen($v)>0){
echo "<img src='http://i3.ytimg.com/vi/$v/default.jpg' width='150' />";
}
}
?>
<?php
getYoutubeImage("http://www.youtube.com/watch?v=CXWSlho1mMY&feature=plcp");
?>
回答by genesis
$url = "http://youtube.com/watch?v=B4CRkpBGQzU";
$url = explode("&", $url);
$vidID = preg_match("|?v=(.*)|", $url);
$thumb_default = file_get_contents("http://img.youtube.com/vi/$vidID/default.jpg");
$thumb1 = file_get_contents("http://img.youtube.com/vi/$vidID/0.jpg");
$thumb2 = file_get_contents("http://img.youtube.com/vi/$vidID/1.jpg");
$thumb3 = file_get_contents("http://img.youtube.com/vi/$vidID/2.jpg");
$thumb4 = file_get_contents("http://img.youtube.com/vi/$vidID/3.jpg");
回答by Mike
Assuming youtube doesn't change their format:
假设 youtube 没有改变它们的格式:
$thumb = preg_match('/watch\?v=(.*)&feature/',$url, $matches);
$thumb = preg_match('/watch\?v=(.*)&feature/',$url, $matches);
This will get you the 'B4CRkpBGQzU' part in $matches[1];
这将使您获得“B4CRkpBGQzU”部分 $matches[1];
回答by shivaP
Getting the video ID from the URL
从 URL 获取视频 ID
$url = "http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=relate";
parse_str( parse_url( $url, PHP_URL_QUERY ) );
echo $vidID;//this is video id
Getting the images
获取图像
$thumb1 = file_get_contents("http://img.youtube.com/vi/$vidID/0.jpg");
$thumb2 = file_get_contents("http://img.youtube.com/vi/$vidID/1.jpg");
$thumb3 = file_get_contents("http://img.youtube.com/vi/$vidID/2.jpg");
$thumb4 = file_get_contents("http://img.youtube.com/vi/$vidID/3.jpg");