Skip to content

Instantly share code, notes, and snippets.

@ConnorMcF
Created October 31, 2015 18:44
Show Gist options
  • Save ConnorMcF/b03e60d190f06ef40e21 to your computer and use it in GitHub Desktop.
Save ConnorMcF/b03e60d190f06ef40e21 to your computer and use it in GitHub Desktop.
Get YouTube title from ID - v3 API
function youtube_title($id) {
// returns a single line of JSON that contains the video title. Not a giant request.
$videoTitle = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=".$id."&key=YOUR_API_KEY&fields=items(id,snippet(title),statistics)&part=snippet,statistics");
// despite @ suppress, it will be false if it fails
if ($videoTitle) {
$json = json_decode($videoTitle, true);
return $json['items'][0]['snippet']['title'];
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment