YouTube API

YouTube is an excellent site for watching videos online. Not only that, it provides an excellent developers API so that you can access YouTube data in your own applications.

Recently I've written a little PHP script that makes use of the API. One function, getVideoId, takes a search term and returns a YouTube video ID for the first matching result. Here is the code:

// return the video ID of the first video returned when searching for $search
function getVideoId($search, $devId)
    $url = "{$devId}&tag=" . urlencode($search) . "&page=1&per_page=1";

    $page = file_get_contents($url);
    $video_id = substr($page, strpos($page, "<id>") + 4);
    $video_id = substr($video_id, 0, strpos($video_id, "</id>"));
    return $video_id;
Posted on 11 Oct 2007
If you enjoyed reading this post you might want to follow @coderholic on twitter or browse though the full blog archive.