Last active
June 14, 2024 21:23
-
-
Save james2doyle/9210212 to your computer and use it in GitHub Desktop.
A PHP function to get youtube video title by just passing the video ID
This file contains hidden or 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 | |
| function youtube_title(string $id) { | |
| // $id = 'YOUTUBE_ID'; | |
| $API_KEY = "YOUR-API-KEY"; | |
| $videoList = @json_decode(@file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id={$id}&key={$API_KEY}")); | |
| // despite @ suppress, it will be false if it fails | |
| if ($videoTitle) { | |
| // look for that title tag and get the insides | |
| preg_match("/<title>(.+?)<\/title>/is", $videoTitle, $titleOfVideo); | |
| return $titleOfVideo[1]; | |
| } else { | |
| return false; | |
| } | |
| // usage: | |
| // $item = youtube_title('zgNJnBKMRNw'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It changed: