Get Youtube Video Info with SimpleXML


Z2pX599xo7M is the video ID

<?php

$sxml = simplexml_load_file(‘http://gdata.youtube.com/feeds/api/videos/Z2pX599xo7M&#8217;);

$media = $sxml->children(‘http://search.yahoo.com/mrss/&#8217;);
$yt   = $media->children(‘http://gdata.youtube.com/schemas/2007&#8217;);

$attr1 = $media->group->thumbnail[0]->attributes();
$thumbnail  = $attr1[‘url’]; // thumbnail url

$attr2 = $yt->duration->attributes();
$length  = $attr2[‘seconds’]; // in seconds
$video_duration = round($length/60,2); // in minutes

?>

One thought on “Get Youtube Video Info with SimpleXML

  1. Nice, that´s what i´m looking for.
    But I´ve one suggestion. With round, you didn´t get the right duration.

    just replace
    $video_duration = round($length/60,2); // in minutes

    with
    $video_duration = floor($length/60).’:’.($length % 60); // in minutes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s