Displaying YouTube Videos with Zend Framework and PHP
on Thursday, 14 April 2011. Posted in PHP, Programming
Automatically Display Your YouTube Videos on Your Website
Introduction
This little tutorial will walk you through adding your YouTube channel videos to your website using the Zend Framework. Some might say that this is easy, all you have to do is copy and paste the code from YouTube into your HTML. That's true, but this basic script does it AutoMagically and that's why we build these bloody machines in the first place. Copying and pasting is so 90's :). Join the 21st century and code a bit.
This tutorial assumes you have already setup a YouTube account and uploaded the videos to your channel. It further assumes you have a PHP capable webserver. We'll download and install the framework from Zend.com on a DreamHost account. How you set it up on your server will depend on the nature of the environment you have. But most should allow this setup without any problem.
Get Zend
Open your trusted browser and navigate to http://framework.zend.com. 
- Click the download link for the latest version of the framework
- Then download the latest documentation so you'll have it for a reference if needed offline.
- Open the archive and extract the library directory
- Upload the library directory to your webserver root folder using sFTP or SCP, in my case on DreamHost, this is something like /home/user001/
Get a YouTube Developers API Key
When you signed up for YouTube you also got a Google account maybe not a Gmail account but at the very least a Google account. With this account you can access various cool utilities from the folks at Google such as the YouTube Developer DashBoard. If you have done so already login to your YouTube account. Now navigate to http://code.google.com/apis/youtube and then click the Developer Dashboard link. 
- Click the New Product button
- Enter a name for the product, I called mine the AH YouTube Video Retriever
- Enter in the website URL if appropriate
- Enter in some description of the Product
- Click the Save button and then a Developers key will be created for you. We'll use this later on to access the API's
Let's Write Some Code
Add the following code to a file called youtubelib.php in the root of your website or in an includes subfolder
<?php
/* Set the path to the Zend Framework */
set_include_path('/home/user/library/');
/* Load the Zend AutoLoad Class */
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
/* Include the YouTube Classes of the Google Data part of the Framework */
require_once('Zend/Gdata/YouTube.php');
require_once('Zend/Gdata/YouTube/VideoEntry.php');
/* My Developers Key */
define('YOUTUBE_API_DEVELOPERKEY', 'AI39si6O9LlYuorQ');
$yt = new Zend_GData_YouTube($httpClient, $applicationId, $clientId, YOUTUBE_API_DEVELOPERKEY);
$videoFeed = $yt->getUserUploads('YourYouTubeChannelNameHere');
$html = '';
foreach ($videoFeed as $videoEntry) {
$html .= '';
$html .= ''.$videoEntry->getVideoTitle().'
';
$html .= '';
$description = $videoEntry->getVideoDescription();
if( isset($description) && $description != "" ){
$html .= '- Description:'. $description .'
';
}
$url = $videoEntry->getFlashPlayerUrl();
$html .= " ";
$html .= '
';
$html .= '';
}
echo $html;
?>
Now just throw some nice html template code around it and include the file like below.
<?php include('includes/head.php'); ?>
<?php include('includes/global.php'); ?>
<?php include('includes/header.php'); ?>
<?php include('includes/navbar.php'); ?>
<?php include('includes/sidebar.php'); ?>
Assured Health Client Testimonials
<?php include('includes/youtubelib.php'); ?>
<?php include('includes/footer.php'); ?>
<?php include('includes/scripts.php'); ?>
</body>
</html>
Here's the result Assured Health - Client Testimonials
There are a lot more calls the API supports but this shows you how easy it is to display your YouTube videos without all that cutting and pasting.








Comments (2)
Kate
John Wicks