如何使用PHP - YouTube API从XML文件中检索内容
问题描述:
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!
请帮助您在检索动态内容时使用PHP和XML。 p>
I 有这段代码: p>
function yt_name(){
$ xmlData = file_get_contents('http://gdata.youtube.com/feeds/api/users/youtube/uploads ?max-results = 5&amp; prettyprint = true');
$ xml = new SimpleXMLElement($ xmlData);
$ yt_name = $ xml-&gt; event-&gt; name;
echo $ yt_name;
}
code> pre>
如何检索 &lt; name&gt; content&lt; / name&gt; code>里面的内容? p>
问候, p>
谢谢! p >
div>
答
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
答
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;
答
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/