返回播放列表缩略图的 YouTube 脚本给出错误


YouTube script that returns playlist thumbnail giving errors?

我在创建一个PHP脚本时得到了很多帮助,该脚本基本上可以从YouTube播放列表中获取视频的最新标题和缩略图。这曾经有效,但不知何故它已经停止工作。有人可以帮忙吗?

php 脚本在 http://new.fearofmobs.com/playlist.php 上运行,我已经打开了错误报告。该脚本不是从YouTube播放列表中返回缩略图并每小时缓存一次。代码

<?php error_reporting(E_ALL ^ E_NOTICE); ini_set('display_errors', 1);?>
<?php
$cachefile = 'videobrowser.txt';
$cache_timer = 3600 + @filemtime($cachefile);// files timestamp + 3600 seconds
if (file_exists($cachefile) && time() < $cache_timer ) {
}
else {
    $data = file_get_contents("http://gdata.youtube.com/feeds/api/playlists/C82EBDAC0429B6A2?orderby=published&max-results=12");
    $fh = fopen($cachefile, 'w') or die("can't open file");
    fwrite($fh, $data);
    fclose($fh);
}
$thumbnail ='';
$data = simplexml_load_file($cachefile);
$xml = simplexml_load_string($data);
foreach($xml->entry as $playlist){
    $media = $playlist->children('http://search.yahoo.com/mrss/');
    $attrs = $media->group->thumbnail[1]->attributes();
    $thumb = $attrs['url'];
    $attrs = $media->group->player->attributes();
    $video = $attrs['url'];
    $title = substr( $media->group->title, 21);
    $url = $video;
    parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
    $vid_Id = $my_array_of_vars['v'];
    ###################NEW CODE
    $toggle = 'hotlink';// replace 'hotlink' with 'null' to save images locally
    if ($toggle == 'hotlink'){
        $image_ID = $thumb;  // hotlink images from YouTube
    }
    else{
        ///////////////////////////////// Save Images To Local Webserver
        ///////////////////////////////// just in case  Youtube objects to hotlinking
        $image_ID = $vid_Id.".jpg"; /// or use sub folder for neatness ->  "images_folder/".$vid_Id.".jpg"
        $image_saved = @filemtime($image_ID);// @ is used to suppress the error caused by the image not having been seen before
        if (!$image_saved){/// if you can't find it on local server go fetch it and save to the sites server
            file_put_contents($image_ID, file_get_contents($thumb));
            //// you can delete the line below
            echo ' fecthed image >> '.$image_ID."<br>" ;
            //// you can delete the line above
        }//// close if image saved
    }        ##################### END NEW CODE
    $thumbnail .= '<div style="float:left; cursor:pointer;">
    <p class="crop"><a class="videobox various iframe" href="http://www.youtube.com/embed/' . $vid_Id . '?autoplay=1&amp;hd=1" onclick=swapper('. $vid_Id .')><img src="' .$image_ID  . '" title="' . $title . '" width="74" height="56"/></a></p></div>';
}
?>
<?php echo $thumbnail; ?>

错误

Warning: simplexml_load_file(): videobrowser.txt:1: parser error : Document is empty in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file():  in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file(): ^ in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file(): videobrowser.txt:1: parser error : Start tag expected, '<' not found in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file():  in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: simplexml_load_file(): ^ in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 16
Warning: Invalid argument supplied for foreach() in /hermes/waloraweb095/b2598/moo.fearofmobscom/fearofmobs2/playlist.php on line 18

如果有人需要任何进一步的信息,请告诉我。

我所知,您没有输入填充videobrowser.txtelse子句:

if (file_exists($cachefile) && time() < $cache_timer ) {
    // you are going here
}
else {
    // but you should be going here
    $data = file_get_contents("http://gdata.youtube.com/feeds/api/playlists/C82EBDAC0429B6A2?orderby=published&max-results=12");
    $fh = fopen($cachefile, 'w') or die("can't open file");
    fwrite($fh, $data);
    fclose($fh);
}

我猜在某个时候videobrowser.txt是在没有内容的情况下创建的(一个完全空的文件)。这意味着您没有重新填充文件,而是期望它被填充。

检查并查看videobrowser.txt是否存在,如果存在,请将其删除。