How do I get the title of a youtube video if I have the Video Id?

Aaron de Windt picture Aaron de Windt · Nov 19, 2009 · Viewed 61.7k times · Source

I'm playing now with the Youtube API and I began a small project (for fun).

The problem Is that I cant find the way to get Title of a video from the Id. (example: ylLzyHk54Z0)

I have looked in the DATA and PLAYER api documentation and I cannot find it.

If someone knows how to do this or if someone could help me find the way to do this, please help me.

NOTE: I'm using javascript. It will be a web app.

EDIT: I have got an Idea. Maybe using a Regular expresion to parse out the title from the page title. I'm working on this.

Answer

SimonDever picture SimonDever · Nov 19, 2009

Not entirely possible in javascript since you are trying to get a document from a different domain. If you are happy to throw in a bit of php try this. Tested ok:

<?
    $vidID = $_POST['vidID'];
    $url = "http://gdata.youtube.com/feeds/api/videos/". $vidID;
    $doc = new DOMDocument;
    $doc->load($url);
    $title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
?>

<html>
    <head>
        <title>Get Video Name</title>
    </head>
    <body>
        <form action="test.php" method="post">
            <input type="text" value="ID Here" name="vidID" />
            <input type="submit" value="Get Name" />
        </form>
        <div id="page">URL: [<?= $url ?>]</div>
        <div id="title">Title: [<?= $title ?>]</div>
    </body>
</html>