Kritical_V Posted July 24, 2012 Share Posted July 24, 2012 Hello, I have been working on a small piece of code for the last day or so, and I have gone through over one hundred resources, and frankly it's beaten me. What I am attempting to do is pull a small bit of data from a XML file served through the youtube api. I have am attempting to pull data from a xml file through a url like the below: https://gdata.youtube.com/feeds/api/playlists/CA22CCE64DD2CCBE?v=2&max-results=1 The code should pull data about latest video from a selected playlist on youtube... <?php // FEED URL $feedURL = 'https://gdata.youtube.com/feeds/api/playlists/CA22CCE64DD2CCBE?v=2&max-results=1'; // read feed into SimpleXML object $sxml = simplexml_load_file($feedURL);?> //Code to output a specific piece of data from the xml file <?php echo $sxml->entry->{'media:group'}{'media:description'}; ?> I have skimmed the fat off the xml file so you can see the piece of data easily that I am trying to output. <?xml version='1.0' encoding='UTF-8'?> <feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/"CUAFSH47eCp7I2A9WhJQEE4."'> <entry gd:etag='W/"YDwqeyM."'> <media:group> <media:player url='https://www.youtube.com/watch?v=dCVmk66ldkE9w&feature=youtube_gdata_player'/></media:group> </entry> </feed> I need my code above to pull the media:player url and echo it out in plain text form. It's seems like I don't no my syntax for working with xml and I guess it's something simple. If anyone can help I would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/266152-someone-help-me-im-loosing-my-mind-simple-xml-not-so-simple/ Share on other sites More sharing options...
gristoi Posted July 24, 2012 Share Posted July 24, 2012 here you go, $xml = "<?xml version='1.0' encoding='UTF-8'?> <feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/"CUAFSH47eCp7I2A9WhJQEE4."'> <entry gd:etag='W/"YDwqeyM."'> <media:group> <media:player url='https://www.youtube.com/watch?v=dCVmk66ldkE9w&feature=youtube_gdata_player'/></media:group> </entry> </feed>"; $sxml = simplexml_load_string($xml); foreach ($sxml->entry as $entry) { // get video player URL $media = $entry->children('http://search.yahoo.com/mrss/'); $attrs = $media->group->player->attributes(); $watch = $attrs['url']; print $watch; } i loaded it from a string, but the principle is exactly the same. hope it helps. this returns : https://www.youtube.com/watch?v=dCVmk66ldkE9w&feature=youtube_gdata_player Quote Link to comment https://forums.phpfreaks.com/topic/266152-someone-help-me-im-loosing-my-mind-simple-xml-not-so-simple/#findComment-1363985 Share on other sites More sharing options...
silkfire Posted July 24, 2012 Share Posted July 24, 2012 SimpleXML is actually very simple if you know how to utilize the library. To echo out the url attribute of the media:player node, use the following code: $feedURL = 'https://gdata.youtube.com/feeds/api/playlists/CA22CCE64DD2CCBE?v=2&max-results=1'; // read feed into SimpleXML object $sxml = simplexml_load_file($feedURL); $url = (string)$sxml->entry->children('media', true)->group->player->attributes()->url; echo $url; Quote Link to comment https://forums.phpfreaks.com/topic/266152-someone-help-me-im-loosing-my-mind-simple-xml-not-so-simple/#findComment-1364076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.