Ever wanted to grab the latest videos from your YouTube channel to display somewhere on your site? This will show you how, using the YouTube API, PHP, and JSON.

Obtain an API key

The first thing you’ll want to do is make sure that you have obtained an API key for use with the YouTube API since we’ll need to access everything we need.

You can see how to obtain a key by following this video:

Get the YouTube channel ID

Next, we need to figure out how to get a YouTube Channel’s ID, which Henry Moshkovich so kindly answered over on StackOverflow:

To obtain the channel id you can view the source code of channel page and find there data-channel-external-id="XXXXXXXXXXXXXXXXXXXXXXXX" this will be the channel id you are looking for!

Get some videos

Now is the fun part — the actual code to use the API to grab the goods. I set a few items variables with the data we need, though you could set anything as a variable and then modularize the setup to be more extendable if you so desire.

$resultsNumber = 'NUMBER_OF_RESULTS';

$requestUrl = 'https://www.googleapis.com/youtube/v3/search?key=' . $apiKey . '&channelId=' . $channelId . '&part=snippet,id&maxResults=' . $resultsNumber .'&order=date';

Using the variables set above and a couple different methods, let’s try to get some videos! First, we’ll try to use file_get_contents, and if that function check fails, we’ll fallback to using cURL. If those both fail we set $json_response = NULL; and use that to output a “no results” message.

// Try file_get_contents first
if( function_exists( file_get_contents ) ) {
	$response = file_get_contents( $requestUrl );
	$json_response = json_decode( $response, TRUE );
} else {
	// No file_get_contents? Use cURL then...
	if( function_exists( 'curl_version' ) ) {
		$curl = curl_init();
		curl_setopt( $curl, CURLOPT_URL, $requestUrl );
		curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, TRUE );
		$response = curl_exec( $curl );
		curl_close( $curl );
		$json_response = json_decode( $response, TRUE );
	} else {
		// Unable to get response if both file_get_contents and cURL fail
		$json_response = NULL;

// If there's a JSON response
if( $json_response ) {
	$i = 1;
	echo '<div class="youtube-channel-videos">';
	foreach( $json_response['items'] as $item ) {
		$videoTitle = $item['snippet']['title'];
		$videoID = $item['id']['videoId'];
		//$videoThumbnail = $item['snippet']['thumbnails']['high']['url'];

		if( $videoTitle && $videoID ) {
			echo '<div class="youtube-channel-video-embed vid-' . $videoID . ' video-' . $i++ . '"><iframe width="500" height="300" src="https://www.youtube.com/embed/' . $videoID . '" frameborder="0" allowfullscreen>' . $videoTitle . '</iframe></div>';
	echo '</div><!-- .youtube-channel-videos -->';

// If there's no response	
} else {
	// Display error message
	echo '<div class="youtube-channel-videos error"><p>No videos are available at this time from the channel specified!</p></div>';

Leave a Reply

Your email address will not be published. Required fields are marked *

Continue Reading