如果我有一个YouTube视频URL,有没有任何方法可以使用PHP和cURL从YouTube API获取相关的缩略图?
当前回答
另一个好的选择是使用YouTube支持的oEmbed API。
您只需将YouTube URL添加到oEmbed URL,就会收到一个JSON,其中包含缩略图和用于嵌入的HTML代码。
例子:
http://www.youtube.com/oembed?format=json&url=http%3A//youtube.com/watch%3Fv%3DxUeJdWYdMmQ
会给你:
{
"height":270,
"width":480,
"title":"example video for 2020",
"thumbnail_width":480,
"html":"...",
"thumbnail_height":360,
"version":"1.0",
"provider_name":"YouTube",
"author_url":"https:\/\/www.youtube.com\/channel\/UCza6VSQUzCON- AzlsrOLwaA",
"thumbnail_url":"https:\/\/i.ytimg.com\/vi\/xUeJdWYdMmQ\/hqdefault.jpg",
"author_name":"Pokics",
"provider_url":"https:\/\/www.youtube.com\/",
"type":"video"
}
有关详细信息,请阅读文档。
其他回答
我认为缩略图有很多答案,但我想添加一些其他URL,以便非常容易地获得YouTube缩略图。我只是从亚萨的回答中提取一些文字。以下是获取YouTube缩略图的一些URL:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/default.jpg
对于缩略图的高质量版本,请使用类似于以下内容的URL:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
还有一个中等质量的缩略图版本,使用与高质量类似的URL:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
对于缩略图的标准定义版本,请使用类似于以下内容的URL:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
对于缩略图的最大分辨率版本,请使用类似于以下内容的URL:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
您可以获取包含视频缩略图URL的视频条目。链接中有示例代码。或者,如果您想解析XML,这里有一些信息。返回的XML有一个media:tumbnail元素,其中包含缩略图的URL。
为了添加/扩展所给出的解决方案,我觉得有必要注意到,正如我自己遇到的问题一样,实际上可以通过一个HTTP请求抓取多个YouTube视频内容,在本例中是缩略图:
使用Rest Client(在本例中为HTTPFUL),您可以执行以下操作:
<?php
header("Content-type", "application/json");
//download the httpfull.phar file from http://phphttpclient.com
include("httpful.phar");
$youtubeVidIds= array("nL-rk4bgJWU", "__kupr7KQos", "UCSynl4WbLQ", "joPjqEGJGqU", "PBwEBjX3D3Q");
$response = \Httpful\Request::get("https://www.googleapis.com/youtube/v3/videos?key=YourAPIKey4&part=snippet&id=".implode (",",$youtubeVidIds)."")
->send();
print ($response);
?>
如果你想要YouTube上某个特定视频ID的最大图片,那么URL应该是这样的:
http://i3.ytimg.com/vi/SomeVideoIDHere/0.jpg
使用API,您可以获取默认缩略图图像。简单代码应该是这样的:
//Grab the default thumbnail image
$attrs = $media->group->thumbnail[1]->attributes();
$thumbnail = $attrs['url'];
$thumbnail = substr($thumbnail, 0, -5);
$thumb1 = $thumbnail."default.jpg";
// Grab the third thumbnail image
$thumb2 = $thumbnail."2.jpg";
// Grab the fourth thumbnail image.
$thumb3 = $thumbnail."3.jpg";
// Using simple cURL to save it your server.
// You can extend the cURL below if you want it as fancy, just like
// the rest of the folks here.
$ch = curl_init ("$thumb1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata = curl_exec($ch);
curl_close($ch);
// Using fwrite to save the above
$fp = fopen("SomeLocationInReferenceToYourScript/AnyNameYouWant.jpg", 'w');
// Write the file
fwrite($fp, $rawdata);
// And then close it.
fclose($fp);
另一个好的选择是使用YouTube支持的oEmbed API。
您只需将YouTube URL添加到oEmbed URL,就会收到一个JSON,其中包含缩略图和用于嵌入的HTML代码。
例子:
http://www.youtube.com/oembed?format=json&url=http%3A//youtube.com/watch%3Fv%3DxUeJdWYdMmQ
会给你:
{
"height":270,
"width":480,
"title":"example video for 2020",
"thumbnail_width":480,
"html":"...",
"thumbnail_height":360,
"version":"1.0",
"provider_name":"YouTube",
"author_url":"https:\/\/www.youtube.com\/channel\/UCza6VSQUzCON- AzlsrOLwaA",
"thumbnail_url":"https:\/\/i.ytimg.com\/vi\/xUeJdWYdMmQ\/hqdefault.jpg",
"author_name":"Pokics",
"provider_url":"https:\/\/www.youtube.com\/",
"type":"video"
}
有关详细信息,请阅读文档。
推荐文章
- 在Bash中将输出赋给变量
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 更好的方法检查变量为null或空字符串?
- 当使用Composer的开发/生产开关时,如何正确部署?
- 自动删除Laravel (Eloquent ORM)中的相关行
- 在取消设置元素后重新设置数组键
- 如何修剪空白的数组值在php