Search code examples
phpapizend-frameworkyoutubezend-gdata

Get Youtube VideoID details in array


I've been struggling with this for hours and i have no idea why is not working. I need to get Details from a VideoID using YouTube API and Zend, so i created a function like this

function listYoutubeVideo($id) {
$videos = array();

try {   
    $yt = new Zend_Gdata_YouTube();


    $videoFeed = $yt->getVideoEntry($id);
    foreach ($videoFeed as $videoEntry) {
        $videoThumbnails = $videoEntry->getVideoThumbnails();
        $videos[] = array(
            'thumbnail' => $videoThumbnails[0]['url'],
            'title' => $videoEntry->getVideoTitle(),
            'description' => $videoEntry->getVideoDescription(),
            'tags' => implode(', ', $videoEntry->getVideoTags()),
            'url' => $videoEntry->getVideoWatchPageUrl(),
            'flash' => $videoEntry->getFlashPlayerUrl(),
            'dura' => $videoEntry->getVideoDuration(),
            'id' => $videoEntry->getVideoId()
        );
    }
} catch (Exception $e) {
}

return $videos;
}

The reason im doing it with an array and a function is because i wanna cache the function.

I have no idea what is wrong with the code, i use exactly the same one just changing getVideoEntry for other types of feeds and it works.


Solution

  • I duplicated your code and ran it. Now getVideoEntry seems to be returning a single video's data, but for some reason you expect it to be a collection? Also if you cache, you might want to create some check for an empty data return.

    Here is some revised code that worked perfectly for me:

    function listYoutubeVideo($id) {
        $video = array();
    
        try {   
            $yt = new Zend_Gdata_YouTube();
    
            $videoEntry = $yt->getVideoEntry($id);
    
                $videoThumbnails = $videoEntry->getVideoThumbnails();
                $video = array(
                    'thumbnail' => $videoThumbnails[0]['url'],
                    'title' => $videoEntry->getVideoTitle(),
                    'description' => $videoEntry->getVideoDescription(),
                    'tags' => implode(', ', $videoEntry->getVideoTags()),
                    'url' => $videoEntry->getVideoWatchPageUrl(),
                    'flash' => $videoEntry->getFlashPlayerUrl(),
                    'dura' => $videoEntry->getVideoDuration(),
                    'id' => $videoEntry->getVideoId()
                );
    
        } catch (Exception $e) {
            /*
            echo $e->getMessage();
            exit();
            */
        }
    
        return $video;
    }