我正在寻找一种从 XML 访问中查找参数“持续时间”值的方法
来源: http: //gdata.youtube.com/feeds/api/videos/bSCs7NzghSg
我尝试了类似的东西:
preg_match('#<yt:duration seconds=\'(.*?)\'/>#is',$xml,$resultduration);
$duration = $resultTitre[count($resultduration)-1];
但值返回 0
我正在寻找一种从 XML 访问中查找参数“持续时间”值的方法
来源: http: //gdata.youtube.com/feeds/api/videos/bSCs7NzghSg
我尝试了类似的东西:
preg_match('#<yt:duration seconds=\'(.*?)\'/>#is',$xml,$resultduration);
$duration = $resultTitre[count($resultduration)-1];
但值返回 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
$file = ' http://gdata.youtube.com/feeds/api/videos/bSCs7NzghSg ';
$xml = simplexml_load_file($file);
$result = $xml->xpath('//yt:duration');
foreach ($result 作为 $node) {
echo (double) $node['seconds'];
echo ' ';
}