【问题标题】:Retrieve YouTube video details, including description from video URL using PHP?使用 PHP 检索 YouTube 视频详细信息,包括来自视频 URL 的描述?
【发布时间】:2016-01-10 04:44:15
【问题描述】:

搜索stackoverflow后发现:How can I retrieve YouTube video details from video URL using PHP?

使用以下代码(我已更改为 https 而不是 http,还添加了 $_GET['v'] 用于从浏览器 URL 获取视频代码):

function get_youtube($url) {

 $youtube = "https://www.youtube.com/oembed?url=". $url ."&format=json";

 $curl = curl_init($youtube);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $return = curl_exec($curl);
 curl_close($curl);
 return json_decode($return, true);

}

$url = 'https://www.youtube.com/watch?v=' . $_GET['v'];

// Display Data 
echo '<pre>';
print_r(get_youtube($url));
echo '</pre>';

我能够得到以下结果:

Array
(
    [thumbnail_url] => https://i.ytimg.com/vi/AhN5MbTJ0pk/hqdefault.jpg
    [version] => 1.0
    [type] => video
    [html] => <iframe width="480" height="270" src="https://www.youtube.com/embed/AhN5MbTJ0pk?feature=oembed" frameborder="0" allowfullscreen></iframe>
    [provider_url] => https://www.youtube.com/
    [thumbnail_width] => 480
    [width] => 480
    [thumbnail_height] => 360
    [author_url] => https://www.youtube.com/user/AndreasChoice
    [author_name] => AndreasChoice
    [title] => GROSS SMOOTHIE CHALLENGE! ft. Tealaxx2
    [height] => 270
    [provider_name] => YouTube
)

这很好,但我还需要检索丢失的视频的完整“描述”。我怎样才能做到这一点?谢谢。

【问题讨论】:

  • 你看过API specification吗?
  • 是的。但这并没有帮助,对我来说都是希腊语。

标签: php youtube-api


【解决方案1】:

要接收视频说明,您有 2 个选项。

  1. 使用 API。
  2. 抓取网站。

您需要的 API 在googleapis.com 域下。

您需要使用的网址是:

https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID&key=YOUR_API_KEY&fields=items(id,snippet(description))&part=snippet

请注意,您必须更改 VIDEO_IDYOUR_API_KEY
要获取 API 密钥,请按照以下说明操作:link


构建网络爬虫更加复杂。
尝试按照本教程构建您自己的网络爬虫here

【讨论】:

  • 谢谢!这正是我想要的。
猜你喜欢
  • 2012-06-09
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 2014-04-20
  • 1970-01-01
  • 2015-02-23
  • 2015-05-14
相关资源
最近更新 更多