Get Youtube VideoID details in array

别等时光非礼了梦想. 提交于 2019-12-10 15:29:18

问题


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.


回答1:


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



回答2:


This is a bug in Zend framework:

http://framework.zend.com/issues/browse/ZF-12461?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel#issue-tabs

For a fast fix you can edit Zend/Gdata/YouTube/VideoEntry.php

line 587:

// $videoId = substr($fullId, $position + 1);
$url = $this->getFlashPlayerUrl();
$videoId = substr($url, 25, 11);

It's not fancy but gets the job done.



来源:https://stackoverflow.com/questions/4653894/get-youtube-videoid-details-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!