Created
May 11, 2023 01:08
-
-
Save everaldomatias/2c39d01a558a9357bda9f7994ccff8f3 to your computer and use it in GitHub Desktop.
Get data from YouTube video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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