如何使用 PHP 从 XML 文件中检索内容 - YouTube API

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

请帮助使用 PHP 和 XML 检索动态内容。

我有这个代码:

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;

}

如何检索 <name>content</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;

输出:YouTube Spotlight

我不确定你的 XML 格式是什么样的,但像这样的格式应该可以 -

$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;

不要再使用它了,V2 API 自 2014 年 4 月起已弃用,将于 2015 年 4 月 21 日关闭

使用 json V3 API https://developers.google.com/youtube/v3/