如果我有一个YouTube视频URL,有没有任何方法可以使用PHP和cURL从YouTube API获取相关的缩略图?


当前回答

Use:

https://www.googleapis.com/youtube/v3/videoCategories?part=snippet,id&maxResults=100&regionCode=us&key=**Your YouTube ID**

上面是链接。使用它,你可以找到YouTube视频的特点。找到特征后,您可以获得所选类别的视频。之后,您可以使用Asaph的答案找到选定的视频图像。

尝试以上方法,您可以解析YouTube API中的所有内容。

其他回答

在YouTube Data API v3中,您可以使用videos->list函数获取视频的缩略图。从snippet.t缩略图.(key)中,您可以选择默认、中等或高分辨率缩略图,并获取其宽度、高度和URL。

您还可以使用缩略图->设置功能更新缩略图。

例如,您可以查看YouTube API示例项目。(PHP版本。)

为了添加/扩展所给出的解决方案,我觉得有必要注意到,正如我自己遇到的问题一样,实际上可以通过一个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 API版本3在2分钟内启动并运行

如果您只想搜索YouTube并获取相关的财产:

获取公共API--此链接提供了良好的方向使用以下查询字符串。出于示例目的,URL字符串中的搜索查询(由q=表示)是stackoverflow。然后YouTube将向您发送一个JSON回复,您可以在其中解析缩略图、代码段、作者等。https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=50&q=stackoverflow&key=YOUR_API_KEY_HERE

我认为缩略图有很多答案,但我想添加一些其他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

另一个好的选择是使用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"
}

有关详细信息,请阅读文档。