Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Created May 11, 2023 01:08
Show Gist options
  • Save everaldomatias/2c39d01a558a9357bda9f7994ccff8f3 to your computer and use it in GitHub Desktop.
Save everaldomatias/2c39d01a558a9357bda9f7994ccff8f3 to your computer and use it in GitHub Desktop.
Get data from YouTube video
<?php
$api_key = 'YOUR_API_KEY';
$url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
$pattern = '/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/';
preg_match($pattern, $url, $matches);
$video_id = $matches[7];
$url = "https://www.googleapis.com/youtube/v3/videos?id=$video_id&key=$api_key&part=snippet";
$data = file_get_contents($url);
$result = json_decode($data, true);
$title = $result['items'][0]['snippet']['title'];
$description = $result['items'][0]['snippet']['description'];
$thumbnail = $result['items'][0]['snippet']['thumbnails']['medium']['url'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment