【问题标题】:YouTube script that returns playlist thumbnail giving errors?YouTube脚本返回播放列表缩略图给出错误?
【发布时间】:2012-08-07 19:24:37
【问题描述】:

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

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

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

【问题讨论】:

  • 欢迎来到 StackOverflow!您的错误消息似乎很清楚 - what have you tried?
  • 你好,它说文件是空的,这很清楚。但是脚本并不是要在其中填充信息,但为什么不是呢?谢谢!
  • 查看谁是您的主机是 1&1。使用共享帐户是否安全?
  • 如果是这样,请确保您的 .htacess 文件没有在任何地方更改或将这一行放在其中:AddHandler x-mapp-php6 .php
  • 据我所知,videobrowser.txt 文件是空的(或者无法从浏览器中看到)。 filemtime 输出什么?

标签: php youtube simplexml


【解决方案1】:

据我所知,您没有输入填充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是否存在,如果存在,删除它。

【讨论】:

  • 嗨@gordon 我现在已经这样做了,错误更少了,这次重新生成并填充了文件:)。现在仍然收到此错误,有什么想法吗? new.fearofmobs.com/playlist.php谢谢!
  • 嗨,我不太确定。也许您正在获取的 XML 的格式已更改?你能确认$xml 被设置为你认为的样子吗?
  • 嗨@gordon,我不认为这会是问题,因为它以前工作过。嗯,我被难住了!
  • 我也不确定,我没有看到明显的错误。我认为这与您最初的问题有所不同。既然我的回答解决了你原来的问题,你介意接受它吗(点击upvote/downvote按钮的复选标记)?尝试自己调试这个新问题,如果找不到解决方案,请打开一个新问题。
猜你喜欢
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 2021-12-21
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
相关资源
最近更新 更多