2013-05-31 50 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