我正在寻找一种从XML访问
中查找参数'duration'的值的方法资料来源: http://gdata.youtube.com/feeds/api/videos/bSCs7NzghSg
我尝试过类似的事情:
preg_match('#<yt:duration seconds=\'(.*?)\'/>#is',$xml,$resultduration);
$duration = $resultTitre[count($resultduration)-1];
但值返回0
答案 0 :(得分:0)
这可以使用SimpleXML完成。使用children()
方法查找子项,使用attributes()
方法访问属性。
这是一个用于此目的的功能:
function getDuration($url) {
$xml = simplexml_load_file($url);
$duration = $xml->children('media', true)
->group->children('yt', true)
->duration->attributes('', true)->seconds;
return $duration;
}
用法:
$url = 'http://gdata.youtube.com/feeds/api/videos/bSCs7NzghSg';
echo getDuration($url); // => 2461
答案 1 :(得分:0)
$ file ='http://gdata.youtube.com/feeds/api/videos/bSCs7NzghSg';
$ xml = simplexml_load_file($ file);
$ result = $ xml-&gt; xpath('// yt:duration');
foreach($ result as $ node){
echo (double) $node['seconds'];
echo ' ';
}