I\'m trying to get the $xml->entry->yt:statistics->attributes()->viewCount attribute, and I\'ve tried some stuff with SimpleXML, and I can\'t really
The yt: prefix marks that element as being in a different "XML namespace" from the rest of the document. You have to tell SimpleXML to switch to that namespace using the ->children() method.
The line you were attempting should actually look like this:
echo (string)$xml->entry[0]->children('yt', true)->statistics->attributes(NULL)->viewCount;
To break this down:
(string) - this is just a good habit: you want the string contents of the attribute, not a SimpleXML object representing it$xml->entry[0] - as expected->children('yt', true) - switch to the namespace with the local alias 'yt'->statistics - as expected->attributes(NULL) - technically, the attribute "viewCount" is back in the default namespace, because it is not prefixed with "yt:", so we have to switch back in order to see it->viewCount - running ->attributes() gives us nothing but attributes, which are accessed with ->foo not ['foo']