2012-03-16 1 views
1

저는 Zend 프레임 워크를 사용하여 YouTube Data 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; 
} 

* 편집 **

내 문제의 Part 1은 global $yt을 포함하여 해결되었습니다. 위의 코드는 더 이상 다음 오류를 반환하지 않습니다 : Fatal error: Call to a member function updateEntry() on a non-object.

이제 남은 문제는 비디오를 비공개로 설정하지 않습니다. 예제의 setVideoDescription으로 테스트해도 아무런 변화가 없습니다 ... 오류가없고 변경도 없습니다. 또한, 네, 저는 5 번이 넘어요. :).

아이디어가 있으십니까?

**

내 자신의 문제를 해결 * 편집 V2. 다른 사람들이이 문제를 겪을 경우에 대비하여 위의 코드를 수정하여 솔루션을 반영했습니다.

답변

2

$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); 
} 
+0

Duh! 저는 글로벌 $ yt를 넣었습니다. 함수 외부. 고맙습니다! 그러나 이렇게하면 오류가 사라지지만 $ yt-> updateEntry는 작동하지 않습니다. setVideoPrivate를 사용하여 위의 링크의 setVideoDescription을 사용하여 시도했지만 어느 것도 변경하지 않았습니다. – Kate

+0

두 번째 부분을 가져 주셔서 감사합니다 ... 몇 가지 기능에서 $ playlistVideoFeed를 사용하고 있습니다. – Kate

+0

@Kate 별도의 질문으로 입력해야하는 별도의 문제입니다. 그러나 아마도'$ videoEntry-> getEditLink()'는 당신이 기대하는 것을 반환하지 않을 것이다. –

관련 문제