Kate
Kate

Reputation: 120

Zend YouTube API $yt->updateEntry not updating video

I'm using the Zend framework to access the YouTube Data API. The function below loops through all the videos in a specific playlist on my account and tallies the view count. Right now I've only got one video in there.

When the view count reaches a certain number (for my testing purposes, 5 views), I'd like to set the video to Private.

I'm using this code example: 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;
}

*EDIT**

Part 1 of my issue has been solved by including the global $yt. The above code no longer returns the following error: Fatal error: Call to a member function updateEntry() on a non-object.

Now, the remaining problem: This doesn't make the video private. Testing with the example's setVideoDescription also doesn't do anything...no errors, no changes. Also, yes, I'm over 5 views :).

Any ideas?

*EDIT v2**

Solved my own issue. I updated the above code to reflect my solution, in case anyone else comes across this.

Upvotes: 1

Views: 1390

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270767

$yt is not in scope inside the function. If you need to access it inside, use the global keyword:

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

Or use the $GLOBALS array:

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

Or best of all, pass it to the function:

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

Since you are passing in $yt, you don't need to separately pass $playlistVideoFeed. Instead, you can create it inside the function:

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

Upvotes: 2

Related Questions