Simple example on how to retrieve and display video title, description and some other associated data from YouTube video.
First step – get the API key
YouTube API key can be obtained for free from this address: https://console.developers.google.com/home/dashboard
A bare-bone code example
<?php $videoid = 'VIDEO_ID'; $apikey = 'YOUR_API_KEY'; $json = file_get_contents('https://www.googleapis.com/youtube/v3/videos?id='.$videoid.'&key='.$apikey.'&part=snippet'); $ytdata = json_decode($json); echo '<h1>Title: ' . $ytdata->items->snippet->title . '</h1>'; echo 'Description: ' . $ytdata->items->snippet->description;
Use var_dump($ytdata->items->snippet); to see other available parameters.
Lots of thanks for this tiny snippet. Read all google docs for hours but haven’t made it running, now it works nearly perfectly!!!
One question, maybe you ca help me about this: How can i get the title or description in a translated version? I have some translations for a dummy clip in english and german. When viewing on youtube, it works fine, the browser-lang is taken as given in accept-language.
But I cant gather a translated description on my website. passing the param hl in the iframe-src just toggles ui-language of the player
Thanks a los and greetings from Spain,
Ok, found it finally and will share the answer to my own question 😉
To gather localized translation you set up in youtube, simply use:
Hi i’m from Brazil and i loved your tutorial, but please how i can get the views from the videos ?
Hi, Pierre! How did you get the view count using the code?
Hi, i solved the last problem but i want to know how i can get the profile picture from any channel from youtube… if you know please help me !
Thanks. I found this very useful
From last 2day i am searching code for fetch youtube video title, after 2 days i find out ..thanks man,,, great tuts…
file_get_contents is not working in codeigniter framework, it shows below error:
Message: file_get_contents(https://www.googleapis.com/youtube/v3/videos?id=FrNOD-mFkCw&key=AIzaSyB4cWTgOhNypE6Jyw7i17BZklIYL7yZ85M&part=snippet): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
I have try some methode, but this methode help me so much..
I also can combine with multiple api key using random array.. 😀