0

I am using using PHP and XMLDOM to retrieve data about a youtube video. Retrieving data from the generic elements is fine such as: <title></title>. But I don't have any idea how to get the media namespace e.g: <media:statistics viewCount="1234567" />. How would I get the value of viewCount?

Here is my code:

/* Retrieve information for YouTube video */
$ytUser = "http://gdata.youtube.com/feeds/api/users/".$user."/uploads?orderby=published&start-index=1&max-results=1"; 

// Read feed in to DOM
$doc = new DOMDocument( '1.0', 'utf-8' );
$doc->load( $ytUser );
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
// Video Author 
$ytAuthor = $doc->getElementsByTagName( "name" )->item( 0 )->nodeValue;
// YT title 
$ytTitle = $doc->getElementsByTagName( "title" )->item( 0 )->nodeValue;
// YT description 
$des = $doc->getElementsByTagName( "description" )->item( 0 )->nodeValue;
$ytDescription = ( strlen( $des ) > 350 ) ? substr( $des, 0, 350 ) . '...' : $des;
Sygon
  • 222
  • 5
  • 15
  • It's just DOM, not XMLDOM. When you use any of the `load*` methods, the ctor arguments will get discarded, so you dont need them in your code. `preserveWhiteSpace` has to go before `load` and `formatOutput` after both. How to fetch elements with namespaces has been answered multiple times before but since `getElementsByTagName` gets by *local name*, you can simply get statistics anyway. – Gordon Feb 20 '12 at 13:59
  • 1
    see http://codepad.viper-7.com/fhp5z3 – Gordon Feb 20 '12 at 14:18
  • possible duplicate of [Grabbing the href attribute of an element](http://stackoverflow.com/questions/3820666/regular-expression-for-grabbing-the-href-attribute-of-an-a-element/3820783#3820783) – Gordon Feb 20 '12 at 14:20
  • I'm still receiving the error of `Call to a member function getAttribute() on a non-object` – Sygon Feb 20 '12 at 14:26
  • then you are either not following along the lines of the example code I gave above because as you can see my example works or the feed of the user you are trying to fetch doesnt have a statistics element. – Gordon Feb 20 '12 at 14:35
  • Thank you I got it working, just realised I had my variables named wrongly. Thanks for your help Gordon. – Sygon Feb 20 '12 at 14:37
  • Also just curious why I got a vote down :) – Sygon Feb 20 '12 at 14:39

1 Answers1

0

You should use the method getElementsByTagNameNS, like this:

$ns = 'the-complete-namespace-uri-for-media';
$media_statistics = $doc->getElementsByTagNameNS( $ns , 'statistics' );

See the documentation.

linepogl
  • 9,147
  • 4
  • 34
  • 45
  • Thanks for the reply, I'm not too sure what would go in the namespace uri. Could you give me an example? `` This is the namespace but I wouldn't know the values of the two attributes as they will be generated depending on the video – Sygon Feb 20 '12 at 13:59
  • The prefix `yt:` is not the actual namespace, but just an abbreviation. The actual one can be found as an attribute to the root element of the xml document. Look for the attribute `xmlns:yt="..."`. – linepogl Feb 20 '12 at 14:03
  • I understand now, but I don't know how to get the value of viewCount. I have this so far: `$ns = "xmlns:yt='http://gdata.youtube.com/schemas/2007"; $stats = $doc->getElementsByTagNameNS( $ns , 'yt:statistics' );` – Sygon Feb 20 '12 at 14:07
  • Once you have the element, use `getAttribute()` as usual, ie, `$stats->item(0)->getAttribute('viewCount')`. – linepogl Feb 20 '12 at 14:11
  • If all the OP wants is getting the statistics element, s/he can use `getElementsByTagName` because that will only consider the local name, e.g. `getElementsByTagName('statistics')` will return the yt:statistics node. You only need `getElementsByTagNameNS` when you are looking for elements in a particular namespace. – Gordon Feb 20 '12 at 14:14
  • When I try to use the getAttribute I get the error: `Fatal error: Call to a member function getAttribute() on a non-object` – Sygon Feb 20 '12 at 14:14
  • @Gordon I am trying to get the value of an attribute inside a namespace. For e.g: `` – Sygon Feb 20 '12 at 14:16
  • @Jack I have added a working example to the comments below your question. – Gordon Feb 20 '12 at 14:20