How to get Youtube Video Keywords list

Hi. Is there any way to get the keywords of a video and display it using PHP? I’m parsing Youtube API using SimpleXML. I can’t seem to display the keywords and can’t find anything on google. Does anybody know how?



If you do not get a reply here, then I imagine the best place to look would be the corresponding Google Group - every Google API I have used has a matching Google support Group set up.

We can’t help you with your broken code if you’re not going to show it to us. (:

I was able to make it work.

I var_dump’ed the SimpleXML object I had and the keyword list wasn’t there. But, from the Youtube API, it supposed be there.

So what I did is I used cURL, substr and strpos. Haha! Crude I know! What gets the job done I guess.


I think it would be worthwhile to stick with SimpleXML. You stumbled into a common problem, whether you know it or not. I don’t know how much you know about XML, so if there are any key words here that don’t make sense then please take a little time to learn about them; they’re essential.

The problem is that SimpleXML can be a little, to put it bluntly, difficult when it comes to working with namespaced items in your XML. These can be recognised by the form [COLOR="#800000"]ns:example[/COLOR] where [COLOR="#800000"]ns[/COLOR] is what’s called the namespace prefix and [COLOR="#800000"]example[/COLOR] is the local name of an element or attribute.

Namespaces, to massively generalise, group XML items together and to avoid ambiguity for items with the same name. Working with some XML returned from the YouTube API (see below), you can hopefully recognise a bunch of namespaces. The XML defines the namespaces in the document element (entry) and look like [COLOR="#800000"]xmlns:prefixname=""[/COLOR]. These definitions declare the prefix to be used within the document and the URI (Uniform Resource Identifier) that a given prefix belongs to. URIs uniquely identify any particular namespace: items using a namespace belong to that (and only that) one namespace. Widely used namespaces (such as the “Media RSS” namespace at commonly use the same prefix across XML documents (e.g. [COLOR="#800000"]media[/COLOR]) more as a matter of convention than requirement; if you see [COLOR="#800000"]media:[/COLOR] it is often for Media RSS (you can always check by looking at the namespace declaration).

Moving swiftly back to SimpleXML, there are a couple of ways to access the namespaced items but as you have seen, the usual [COLOR="#800000"]$element->child->grandchild[/COLOR] syntax does not work. You have to say to SimpleXML, “okay, I want to work with this namespace now, thanks!” For this, there is a dedicated method available on all [COLOR="#800000"]SimpleXMLElement[/COLOR] objects called [COLOR="#800000"]children()[/COLOR] (docs [FONT=Book Antiqua][I]&[/I][/FONT] [url=]examples).

Lets have an example, we’ll use this XML which contains video information for [url=][I][FONT=Book Antiqua]Sesame Street: Ray Charles Sings “I Got A Song” With Bert & Ernie[/FONT][/I]. The XML looks like:

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="" xmlns:media="" xmlns:gd="" xmlns:yt="" gd:etag="W/&quot;A0UFQX47eCp7I2A9WhRSGUo.&quot;">
  &#8230; more stuff here&#8230;
    <media:category label="Entertainment" scheme="">Entertainment</media:category>
    &#8230; more stuff here&#8230;
    <media:keywords>sesame, street, celebrity, ray, charles, ernie, bert, muppet, instruments, guitar, drums, piano</media:keywords>
    <media:license type="text/html" href="">youtube</media:license>
    <media:player url=";feature=youtube_gdata_player"/>
    <media:thumbnail url="" height="90" width="120" time="00:01:04.500" yt:name="default"/>
    &#8230; more stuff here&#8230;
  <gd:rating average="4.9522257" max="5" min="1" numRaters="921" rel=""/>
  <yt:statistics favoriteCount="2149" viewCount="610256"/>
  <yt:rating numDislikes="11" numLikes="910"/>

The keywords that you’re after are held within [COLOR="#800000"]media:keywords[/COLOR] tags, which means that the local name is [COLOR="#800000"]keywords[/COLOR] (a logical enough name to hold the keywords!) within the Media RSS namespace (defined with the prefix [COLOR="#800000"]media[/COLOR] at the top of the XML). The children() method allows use to access items within a given namespace by using either the URI ([COLOR="#800000"]children("")[/COLOR]) or the prefix ([COLOR="#800000"]children("media", TRUE)[/COLOR], the second argument tells children that you’re giving it a prefix).

The structure of the XML, to access the keywords, looks like

  &#9492; media:group
      &#9492; media:keywords

In SimpleXML, that would look like

$entry->children("media", TRUE)->group->keywords

In words, if [COLOR="#800000"]$entry[/COLOR] is the result of loading the XML, that says: give me my [COLOR="#800000"]media[/COLOR] namespaced child elements, get the first [COLOR="#800000"]group[/COLOR] from those children, and get the first [COLOR="#800000"]keywords[/COLOR] (still in the [COLOR="#800000"]media[/COLOR] namespace) from that [COLOR="#800000"]group[/COLOR].

Putting all of that together into a simple example, getting an array of the keywords for that video might look like.


$entry = simplexml_load_file('');
$keywords = (string) $entry->children('media', TRUE)->group->keywords;

$keywords_array = explode(", ", $keywords);


The example should output something like the following if all goes well.

    [0] => sesame
    [1] => street
    [2] => celebrity
    [3] => ray
    [4] => charles
    [5] => ernie
    [6] => bert
    [7] => muppet
    [8] => instruments
    [9] => guitar
    [10] => drums
    [11] => piano

More reading:

P.S. As for [COLOR="#800000"]var_dump()[/COLOR] not displaying the namespaced items, unfortunately it just doesn’t. To see what a given element contains, it is generally more useful just to call [COLOR="#800000"]asXML()[/COLOR] on it which will return its XML.

Great explanation of simpleXML and its behaviour concerning namespaces - I did not know that - thanks.