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"); 
}

?>            
4

1 回答 1

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");
于 2013-05-31T22:28:11.633 回答