【问题标题】:Zend YouTube API $yt->updateEntry not updating videoZend YouTube API $yt->updateEntry 不更新视频
【发布时间】:2012-03-16 02:14:35
【问题描述】:

我正在使用 Zend 框架来访问 YouTube 数据 API。下面的函数循环播放我帐户中特定播放列表中的所有视频并计算观看次数。现在我只有一个视频。

当观看次数达到一定数量(出于测试目的,5 次观看)时,我想将视频设置为私有。

我正在使用此代码示例:https://developers.google.com/youtube/2.0/developers_guide_php#Updating_Video_Information

$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, null, $developerKey);

$playlistVideoFeed = $yt->getPlaylistVideoFeed('http://gdata.youtube.com/feeds/api/playlists/XXXXXX');

function playCount($playlistVideoFeed, $yt) {
      $count = 1;
      $totalViews = 0;
      foreach ($playlistVideoFeed as $videoEntry) {

        // ensure the video entry is not private
        if(!$videoEntry->isVideoPrivate()) {        

          // add this episode's play count to the total view count
          $totalViews = $totalViews + $videoEntry->getVideoViewCount();

          // if views are X and I can edit this video, set it to private
          if($totalViews >= 5) {
            $vidID = $videoEntry->getVideoId();
            $videoEntryToEdit = $yt->getFullVideoEntry($vidID);
                if($videoEntryToEdit->getEditLink() !== null) {
                    $putUrl = $videoEntryToEdit->getEditLink()->getHref(); 
                    $videoEntryToEdit->setVideoPublic(); 
                    $yt->updateEntry($videoEntryToEdit, $putUrl); 
                }
          }

          $count++;
        } 
      }
  return $totalViews;
}

*编辑**

我的问题的第 1 部分已通过包含 global $yt 得到解决。以上代码不再返回以下错误:Fatal error: Call to a member function updateEntry() on a non-object

现在,剩下的问题: 这不会将视频设为私有。使用示例的setVideoDescription 进行测试也没有做任何事情......没有错误,没有更改。另外,是的,我的浏览量超过 5 次 :)。

有什么想法吗?

*EDIT v2**

解决了我自己的问题。我更新了上面的代码以反映我的解决方案,以防其他人遇到这个问题。

【问题讨论】:

    标签: php zend-framework youtube-api


    【解决方案1】:

    $yt 不在函数范围内。如果需要在里面访问,使用global关键字:

    function playCount($playlistVideoFeed) {
         // Access the global $yt
         global $yt;
    
         $count = 1;
          $totalViews = 0;
          foreach ($playlistVideoFeed as $videoEntry) {
    
            // ensure the video entry is not private
            if(!$videoEntry->isVideoPrivate()) {        
    
              // add this episode's play count to the total view count
              $totalViews = $totalViews + $videoEntry->getVideoViewCount();
    
              // if views are X and I can edit this video, set it to private
              if($totalViews >= 5 && $videoEntry->getEditLink() !== null) {
                $putUrl = $videoEntry->getEditLink()->getHref();
                $videoEntry->setVideoPrivate();
                $yt->updateEntry($videoEntry, $putUrl);
              }
    
              $count++;
            } 
          }
      return $totalViews;
    }
    

    或者使用$GLOBALS数组:

    $GLOBALS['yt']->updateEntry($videoEntry, $putUrl);
    

    或者最重要的是,将它传递给函数:

    function playCount($playlistVideoFeed, $yt) {
      // function body
      // etc...
      $yt->updateEntry($videoEntry, $putUrl);
    }
    

    由于您传递的是$yt,因此您无需单独传递$playlistVideoFeed。相反,您可以在函数内部创建它:

    function playCount($yt) {
      // get the feed inside, since $yt is inside...
      $playlistVideoFeed =  $yt->getPlaylistVideoFeed('http://gdata.youtube.com/feeds/api/playlists/XXXXXX');
      // function body
      // etc...
      $yt->updateEntry($videoEntry, $putUrl);
    }
    

    【讨论】:

    • 呃!我把全局 $yt;函数之外。谢谢!但是,虽然这使错误消失,但 $yt->updateEntry 不起作用。我用 setVideoPrivate 和上面链接的 setVideoDescription 尝试过,两者都没有变化。
    • 感谢第二部分...我在几个函数中使用了 $playlistVideoFeed,所以将这部分保留在外面似乎最整洁?
    • @Kate 这是一个单独的问题,应该作为单独的问题输入。但也许$videoEntry->getEditLink() 没有返回您所期望的结果。
    猜你喜欢
    • 2022-06-25
    • 2020-12-19
    • 1970-01-01
    • 2020-04-20
    • 2013-08-03
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    相关资源
    最近更新 更多