检索YouTube视频详细信息,包括使用PHP从视频URL的描述


Retrieve YouTube video details, including description from video URL using PHP?

搜索stackoverflow后,我发现:如何使用PHP从视频URL检索YouTube视频详细信息?

使用以下代码(我已更改为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
)

这是伟大的,但我也需要检索完整的"描述"的视频是缺失的。我怎样才能做到这一点呢?谢谢你。

为了接收视频的描述,您有两个选项


  1. 抓取网站

您需要的API位于googleapis.com域下。

你需要使用的url是:

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密钥,请遵循以下说明:


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