2011-04-27 52 views
0

我試着閱讀YouTube XML數據寬度PHP抱住由於YouTube使用冒號(:)他的API 中)準確地說,這裏是我的腳本使用:PHP腳本不能用冒號讀取XML數據(:)

$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:記錄

回答

2

您需要在嘗試訪問它的值之前首先獲取命名空間。看看方法getNamespaces。使用children你就可以訪問值在命名空間裏:

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

我下面在http://de2.php.net/manual/en/simplexmlelement.getnamespaces.php給出的第一個例子。如果我使用與該示例中給出的相同的'xml',它將起作用。但不適用於我的XML。我想分析在'裏面''標籤內容 0.02 0.0 0.0 '任何指針?謝謝 – 2011-07-12 13:20:21

+0

請檢查我的問題http://stackoverflow.com/questions/6665222/unable-to-parse-xml-data-with-colon-from-response-using-getnamespaces – 2011-07-12 13:49:32