Jump to content

Someone help me i'm loosing my mind (Simple XML not so simple)


Kritical_V

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.