Youtube API and cURL

I was recently working with the YouTube API, and was having some frustrations getting a particular piece of data I was looking for. After a bit of Googling, I quickly discovered that other people were having a similar issue. I finally was able to dig up the answer, buried somewhere in an IBM document.

People were struggling with the way the XML was being interpreted by the standard PHP simplexml object, and couldn’t seem to accurately target the media or stats portions of the code. This means it’s difficult to get the video length and view counts.

What I ended up doing, was creating a function that would accept a YouTube username as a parameter, and return an array of video information for all videos that user has uploaded. Here’s that function:

function get_youtube_videos($max_number, $user_name) {
    $xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/' . $user_name . '/uploads?max-results=' . $max_number);

    $server_time = $xml->updated;

    $return = array();

    foreach ($xml->entry as $video) {
        $vid = array();

        $vid['id'] = substr($video->id,42);
        $vid['title'] = $video->title;
        $vid['date'] = $video->published;
        //$vid['desc'] = $video->content;

        // get nodes in media: namespace for media information
        $media = $video->children('http://search.yahoo.com/mrss/');

        // get the video length
        $yt = $media->children('http://gdata.youtube.com/schemas/2007');
        $attrs = $yt->duration->attributes();
        $vid['length'] = $attrs['seconds'];

        // get video thumbnail
        $attrs = $media->group->thumbnail[0]->attributes();
        $vid['thumb'] = $attrs['url'];

        // get <yt:stats> node for viewer statistics
        $yt = $video->children('http://gdata.youtube.com/schemas/2007');
        if (isset($yt->statistics)) {
                $attrs = $yt->statistics->attributes();
                $vid['views'] = $attrs['viewCount'];
        } else {
                $vid['views'] = "0";
        }

        array_push($return, $vid);
    }

    return $return;
}

And here’s the implementation:

$max_videos = 9;
$user_name = 'thelonelyisland';
$videos = get_youtube_videos($max_videos, $user_name);

foreach($videos as $video) {
    echo $video['title'] . '<br/>';
    echo $video['id'] . '<br/>';
    echo $video['date'] . '<br/>';
    echo $video['views'] . '<br/>';
    echo $video['thumb'] . '<br/>';
    echo $video['length'] . '<br/>';
    echo '<hr/>';
}

Update (30 Mar 12, 12:30 AM EST)

Fun fact: Just before this went to a client (IE one of the largest companies in Canada), we discovered it was broken. Turns out that videos with zero views were returning a null value somewhere, causing the function to explode. Since most of their videos where at least a month old, this made it through testing, until they happened to upload a new video just before launch. Close call. I have now updated the post to include the latest, greatest version of this function.

One Response to “Youtube API and cURL”

  1. ArleyM Says:

    I hope all is well! Your email isn’t anywhere on your site you cyber-recluse!

    Just wanted to say hey.


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>