Ismail
Ismail

Reputation: 47

How to retrieve content from XML file using PHP - YouTube API

Please help in using PHP and XML in retrieving dynamic content.

I have this code:

function yt_name() {
$xmlData = file_get_contents( 'http://gdata.youtube.com/feeds/api/users/youtube/uploads?max-results=5&prettyprint=true' ); 
$xml = new SimpleXMLElement($xmlData); 
$yt_name = $xml->event->name;

    echo $yt_name;

}

How can I retrieve the content inside <name>content</name> ?

Regards,

Thank you!

Upvotes: 2

Views: 709

Answers (3)

ceasar
ceasar

Reputation: 1522

Don't use this anymore V2 API is deprecated since april 2014 and will be closed on 21 april 2015

Use the json V3 API https://developers.google.com/youtube/v3/

Upvotes: 2

Michael Cale
Michael Cale

Reputation: 1

I'm not sure what your XML format looks like, but something like this should work -

$XMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<video>
<name>This is the name</name>
<submitter>Mrs. Robinson</submitter>
</video>";

$xml=simplexml_load_string($XMLData) or die("Error");


echo $xml->name;

Upvotes: 0

mend3
mend3

Reputation: 516

Once inside the object, you can retrieve childs by object index (node name):

function yt_name() {
    $xmlData = file_get_contents( 'http://gdata.youtube.com/feeds/api/users/youtube/uploads?max-results=5&prettyprint=true' ); 
    $xml = new SimpleXMLElement($xmlData); 
    return $xml;
}

$yt = yt_name();
echo $yt->author->name;

Output: YouTube Spotlight

Upvotes: 1

Related Questions