2013-05-31 55 views
0

我試圖製作一個代碼,將用戶重定向到基於上傳器的隨機YouTube視頻。該腳本根本不加載,我不知道爲什麼。這裏的腳本簡單數組重定向

<?php 
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USER/uploads?max-results=50'; 
$sxml = simplexml_load_file($feedURL); 
$i=0; 
foreach ($sxml->entry as $entry) { 
     $watch = (string)$media->group->player->attributes()->url; 
     $videoid = substr($watch, 31, -29);  
     $finalid = "\"$videoid\", "; 
     $urls = array ($finalid); 
     $url = $urls[array_rand($urls)]; 
     header("Location: http://www.youtube.com/watch?v=$url&list=UL"); 
} 

?>    
+0

你有沒有做過任何* *測試?這些函數調用是否失敗?檢索的數據是否正確?你有錯誤報告嗎? – Sammitch

+0

是的,我有。如果我回應最終身份證,它工作正常。 – user2398026

+0

我已經編輯了你的代碼的格式來說明你的循環包含的代碼比它應該多得多。此外,您不能以您似乎正在嘗試的方式定義數組,它只會包含用引號括起來的單個字符串,並用逗號分隔。給我一點時間來重寫這個。 – Sammitch

回答

0

您的代碼看起來應該更像是這樣的:

<?php 
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USER/uploads?max-results=50'; 
$sxml = simplexml_load_file($feedURL); 

// check the return value 
if($sxml === false) { die('error retrieving XML data'); } 

// declare an empty array 
$video_ids = array(); 

// loop through returned data 
foreach ($sxml->entry as $entry) { 
    $watch = (string)$media->group->player->attributes()->url; 
    // add each ID to the array 
    $video_ids[] = substr($watch, 31, -29); 
} 

// check that there were some actual videos in that data 
if(empty($video_ids)) { die('No videos returned'); } 

// pick 
$url = $video_ids[array_rand($video_ids)]; 
//redirect 
header("Location: http://www.youtube.com/watch?v=$url&list=UL"); 
+0

我嘗試過,並得到以下錯誤[這是它的一個pastebin](http://pastebin.com/BtxwyH71) – user2398026

+0

您的pastebin建議1.您的供稿網址不正確。 2.你沒有保留返回值的檢查。此外,$ media-> group-> player-> attributes() - > url似乎是不正確的幾個不同的陰影,因爲我剛剛嘗試運行此代碼。提示:'print_r($ sxml)' – Sammitch

+0

你失去了我。我不明白。 – user2398026