php脚本无法用冒号(:)读取xml数据

时间:2011-04-27 08:06:36

标签: php xml namespaces xml-namespaces colon

我试图读取youtube xml数据宽​​度php但卡住因为youtube在他的API中使用了冒号(:) 确切地说,这是我使用的脚本:

$video = array('/some arrays here,separated width commas/');// array of youtube videos

$v = preg_replace("/[^A-Za-z0-9\-\_]/","",$_GET["v"]); // make sure "v" parameter is sanitized

if(!empty($v) && in_array($v,$video)){ //check if parameter "v" is empty and is yours video
    echo('<object width="853" height="505"><param name="movie" value="http://www.youtube.com/v/'.$v.'&hl=en_US&fs=1&hd=1&color1=0xe1600f&color2=0xfebd01"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$v.'&hl=en_US&fs=1&hd=1&color1=0xe1600f&color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="853" height="505"></embed></object>');     
}else{
    foreach($video as $v){
        echo('
        <div class="tube"><a href="/pages/tube.php?v='.$v.'" class="thickbox"><img style="border:2px solid #000;margin:5px;" alt="" src="http://i1.ytimg.com/vi/'.$v.'/default.jpg"/></a>');
        $xmlData = simplexml_load_string(file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$v.'?fields=title,yt:recorded'));//it should be working if I add fields just separated width commas

        $title = (string)$xmlData->title;
        $recorded = (string)$xmlData->recorded;//It should be yt:recorded but I understand that cant use : here!

        {        echo '<p>'.$recorded.' '.$title.'</p></div>';    }
    }
}

它工作得很好,只是标题。有可能以某种方式读取yt:录制

1 个答案:

答案 0 :(得分:2)

在尝试访问命名空间之前,您需要先获取命名空间。看看getNamespaces方法。使用children,您可以访问命名空间内的值:

$namespaces = $entry->getNameSpaces(true);
$yr = $entry->children($namespaces['yt']); 
echo $yr->recorded;