This question already has an answer here:
- SimpleXML and namespaces 3 answers
I'm trying to get video info from a YouTube XML feed such as the title and thumbnail URL.
Let's take this feed as an example:
http://gdata.youtube.com/feeds/api/videos/CevxZvSJLk8
Here's the code I have now:
$videoURL = 'http://gdata.youtube.com/feeds/api/videos/CevxZvSJLk8';
$sxml = simplexml_load_file($videoURL);
print_r($sxml);
However, that doesn't seem to be including the media:group part of the xml sheet.
Essentially, I'm trying to get these two values:
entry -> media:group -> media:title
entry -> media:group -> media:thumbnail
EDIT: This is the solution I ended up going with from reading other similar questions.
$videoTitle = $sxml->children('media', true)->group->title;
$videoThumbnail = $sxml->children('media', true)->group->thumbnail->attributes()->url;
The colon in a tag name separates the namespace prefix from the local element or attribute name. The feed in the question has several namespaces.
http://www.w3.org/2005/Atom
http://purl.org/atom/app#
with prefixapp
http://search.yahoo.com/mrss/
with prefixmedia
http://schemas.google.com/g/2005
with prefixgd
http://gdata.youtube.com/schemas/2007
with prefixyt
The namespace prefix is an alias for the actual namespace, it is only valid for the document and can change for child nodes. The next feed could use the prefix
atom
forhttp://www.w3.org/2005/Atom
. So depending on the namespace prefix from the document is a bad idea.If you use Xpath fo fetch the elements from a document, you can register your own prefixes for the used namespaces. The following example fetches the media rss title and the url of the large thumbnail directly as strings. If an element does not exists, it will return an empty string.
Output: