Retrieving YouTube Channel’s Videos with Zend_Gdata_YouTube

Zend_Gdata

Zend Framework gives you the possibility to interact with Gdata services, which are provided by most of the Goolge services. You can find more on the docs page of Zend_Gdata. The basic principle is that you can connect a service with you API key, given by Google. What I’m about to show you is how do connect such a service.

In theory Zend_Gdata depends on Zend_Http_Client and you’ve to connect it with an instance of this class.

// 1. setup API key
$apiKey = 'your_api_key_here';
 
// 2. retrieve videos via GData Atom
$client = new Zend_Http_Client();
$gdata = new Zend_Gdata_YouTube(null, 'my-app', null, $apiKey);

Note that you’ve to replace your API key in the chunk above.

Now once that you’ve connected the YouTube service you can iterate through the entries from the video feed.

$videoFeed = $gdata->getUserUploads('username');
foreach ($videoFeed as $entry) {
    echo $entry->getTitle();
}

The thing is that you can modify a bit the code above. As ZF docs says if you don’t setup a Zend_Http_Client a default with default configuration, so you can omit this and simple write:

// 1. setup API key
$apiKey = 'your_api_key_here';
 
// 2. retrieve videos via GData Atom
$gdata = new Zend_Gdata_YouTube(null, 'my-app', null, $apiKey);

Related posts:

  1. Check YouTube Video Existence with Zend_Gdata_YouTube
  2. Iterate over YouTube Channel with Zend_Gdata_YouTube
  3. POST with Zend_Http_Client
This entry was posted in micro tutorial, snippets, zend framework and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

10 Responses to Retrieving YouTube Channel’s Videos with Zend_Gdata_YouTube

  1. Claus H. says:

    thumbs up :)

  2. theodor says:

    it throws an error on my machine

  3. Stoimen says:

    Hi,

    can you paste the some code, it may help.

    all the best,
    stoimen

  4. theodor says:

    sure,

    // 1. setup API key
    $key = ‘…’;

    // 2. retrieve videos via GData Atom
    $gdata = new Zend_Gdata_YouTube(‘project’, null, $key);

  5. Stoimen says:

    @theodor – the thing you miss is the first parameter of Zend_Gdata_YouTube constructor. Note that its default value is null, but you’ve to write it even than.

    The code should be changed to:
    $gdata = new Zend_Gdata_YouTube(null, ‘project’, null, $key);

    hope this helps!

  6. Philip says:

    So this is a really stupid ques. I’m trying to figure out how to upload videos to youtube through php api.

    Am I suppose to first install the zend framework, and then put in the code you showed above to connect to youtube and start working with there services?

  7. Stoimen says:

    Well if you’d like to use Zend_Gdata you’ve to install Zend Framework first. However here is not shown how to upload videos, but how to access them via the gdata protocol. You’ve to search the ZF docs for more information.

  8. D says:

    You really dont need to install the whole framework just to use the component. All you need is the Loader component and you will be ready to go. You might have to download the whole framework though and just copy the components you need out of it. ZF is a fully decoupled Framework.

    and on the head of your script you can include the necessary class files as shown below:

    require_once 'Zend/Loader.php'; 
    Zend_Loader::loadClass('Zend_Gdata_AuthSub');
    Zend_Loader::loadClass('Zend_Gdata_YouTube');
    Zend_Loader::loadClass('Zend_Uri_Http');
  9. D says:

    Another point if you intend just reading the public feeds you dont need to enter any parameter for the GData_YouTube constructor – you dont need a developer key or an object of the ClientLogin

  10. Stoimen says:

    @D – sure you don’t need to install the entire framework, … actually you can use it more like a library without the MVC, as you said.
    However it was my case – I already had ZF installed ;)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">