返回播放列表缩略图的YouTube脚本会出错?

时间:2012-08-07 19:24:38

标签: php youtube simplexml

我在创建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

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

1 个答案:

答案 0 :(得分:1)

据我所知,您没有输入填充else的{​​{1}}条款:

videobrowser.txt

我猜测在某些时候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是否存在,如果存在,请将其删除。