2011-08-15 60 views
5

所以,这里是我用于获取YouTube用户的公开播放列表代码:获得完整的播放列表列表的YouTube用户通过API

function getyoutubeplaylists($userName) { 
$yt = connectyoutube(); 
$yt->setMajorProtocolVersion(2); 
$playlistListFeed = $yt->getPlaylistListFeed($userName); 
foreach ($playlistListFeed as $playlistListEntry) { 
    $playlist['title'] = $playlistListEntry->title->text; 
    $playlist['id'] = $playlistListEntry->getPlaylistID(); 
    $playlists[] = $playlist; 
    $playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl()); 
    foreach ($playlistVideoFeed as $videoEntry) { 
     $playlist_assignment['youtube_id'] = substr($videoEntry->getVideoWatchPageUrl(),31,11); 
     $playlist_assignment['id'] = $playlist['id']; 
     $playlist_assignments[] = $playlist_assignment; 
    } 
} 
$everything['playlists'] = $playlists; 
$everything['playlist_assignments'] = $playlist_assignments; 
return $everything; 
} 

问题是,这只是获取第一页或结果。关于如何使用Zend Gdata检索下一页结果的任何想法?

的原始XML GDATA显示的URL需要:

<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=1&amp;max-results=25"/> 
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=26&amp;max-results=25"/> 

然而,getPlaylistListFeed似乎没有任何参数来指定“创业指数”或“最大结果”。

+0

我想应该说,我知道我可以修改getPlaylistListFeed采取相应的表示参数,然后修改URI ,但我以某种方式不认为我在Zend产品中发现了这样一个简单的缺点。 – poolnoodl

回答

2

这是一个非常有用的Zend函数,它不仅用于检索下一页,而且还用于检索Feed的所有可用条目。

$playlistListFeed = $yt->retrieveAllEntriesForFeed($yt->getPlaylistListFeed($userName)); 
1

因为我浪费了一整天“只是试图让我的视频列表显示在我的网站”,我想我会贴上去的,只是教了我很多的一些示例代码。正如所料,Zend_Gdata_YouTube::已经在Magento安装。下面的代码将显示“视频”的统一列表,其中播放列表与其他视频并排显示在列表中。 更新:我做了一个Magento extension out of my answer

这只是一个通用的php我掉进var/export。在顶部,你必须包括app/Mage.php

<? 
$userName='cacycleworksdotcom'; 
$yt = new Zend_Gdata_YouTube(); 
////////////////////////////////////////////////////////////////////// 
// Get playlists. 
$playlistListFeed = $yt->retrieveAllEntriesForFeed($yt->getPlaylistListFeed($userName)); 
$playlist=Array(); 
$videoEntry=NULL; 
$playlistvideos=Array(); 

在这里,您在$playlistListFeed中有一个Zend对象数组。

foreach ($playlistListFeed as $idx=>$playlistListEntry) { 
    // process each playlist 
    $playlists[$idx]['title'] = $playlistListEntry->title->text; 
    $url=$playlistListEntry->getSelfLink()->href; 
    $id=explode("/PL",$url); 
    $playlists[$idx]['id'] = $id[1]; 
    $playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl()); 
    $playlists[$idx]['time']=0; 
    $playlists[$idx]['views']=0; 
    $playlists[$idx]['rating']=0; 

现在,在播放列表中,我看这个播放列表中的视频,并在每一个收集相关统计数据。我总结他们的时间来获得总播放时间,然后从播放列表中的视频中捕捉最高的观看次数和评分。

foreach ($playlistVideoFeed as $videoEntry) { 
     // info of each video inside this playlist 
     $_id=substr($videoEntry->getVideoWatchPageUrl(),31,11); 
     $playlistvideos[]=$_id; 
     $_url=$videoEntry->getVideoWatchPageUrl(); 
     $playlists[$idx]['videos'][$_id]=$_url; 
     $playlists[$idx]['time']+=$videoEntry->getVideoDuration(); 
     $_views=$videoEntry->getVideoViewCount(); 
     if($_views > $playlists[$idx]['views']) 
      $playlists[$idx]['views']=$_views; 
     $_rating=$videoEntry->getRating()->average; 
     if($_rating > $playlists[$idx]['rating']) 
      $playlists[$idx]['rating']=$_rating; 
    } 

我最终使用XML来获取缩略图数据。

$xml=$playlistListEntry->getXML(); 
    // $playlists[$idx]['xml']=$xml; // store original XML for now 
    $xml = simplexml_load_string($xml); // transfer into object 
    $attrs=$xml->group->thumbnail[1]; 
    $playlists[$idx]['thumb']=(string)$attrs['url']; 
    //        1st vid id   playlist id 
    // http://www.youtube.com/watch?v=mcnIAErKc-g&list=PLEDADE9CA0E65BA78 
    $videoid=array_keys($playlists[$idx]['videos']); 
    $videoid=$videoid[0]; 
    $playlists[$idx]['url'] = "http://www.youtube.com/watch?v=$videoid&list=PL".$playlists[$idx]['id'];  
} 

的播放列表处理,现在让我们在其余的视频不在播放列表:

////////////////////////////////////////////////////////////////////// 
// Videos themselves 
$idx=count($playlists); 
$userFeed = $yt->getUserUploads($userName); 
foreach ($userFeed as $videoEntry) { 
    $idx++; 
    $_id=substr($videoEntry->getVideoWatchPageUrl(),31,11); 
    if(! in_array($_id, $playlistvideos)) { 
     $_url=$videoEntry->getVideoWatchPageUrl(); 
     $playlists[$idx]['id']=$_id; 
     $playlists[$idx]['url']=$_url; 
     $playlists[$idx]['title']=$videoEntry->title->text; 
     $playlists[$idx]['views']=$videoEntry->getVideoViewCount(); 
     $playlists[$idx]['rating']=$videoEntry->getRating()->average; 
     $thumbs=$videoEntry->getVideoThumbnails(); 
     // these need resizing to width="320" height="180" 
     $playlists[$idx]['thumb']=$thumbs[0]['url']; 
     $playlists[$idx]['time']=$videoEntry->getVideoDuration(); 
    } // else { echo "$_id already in playlist\n"; } 
} 

在这里,我们有我们的YouTube视频的阵列,在顶部的playlsits通过有序最早的第一个,然后用户的视频不会以相同的最早的第一顺序出现在播放列表中。所以我找到了这个简单的排序代码来改变顺序。那里的大文章关于排序和值得一读,如果你在这里试图排序多维数组。

////////////////////////////////////////////////////////////////////// 
// http://www.the-art-of-web.com/php/sortarray/ 
function orderBy($data, $field) { 
    $code = "return strnatcmp(\$a['$field'], \$b['$field']);"; 
    // swap $a and $b to make descending instead of ascending 
    usort($data, create_function('$b,$a', $code)); //('$a,$b', $code)); 
    return $data; 
} 
$playlists = orderBy($playlists, 'views'); 
////////////////////////////////////////////////////////////////////// 
echo "\n\n"; 
print_r($playlists); 

这里是帮助我开始使用这些愚蠢的GData的YouTube Zend的对象使用的代码:

echo "\n\n"; 
show_methods($videoEntry); 
echo "\n\n"; 
show_methods($playlistListFeed[0]); 
echo "\n\n"; 
show_methods($playlistListFeed); 

function show_methods($_a) { 
    echo "<h3>Methods for ".get_class($_a)."</h3>"; 
    $_a= get_class_methods($_a); 
    $_a=array_unique($_a); 
    array_multisort(&$_a); 
    $i=0; 
    foreach($_a as $method) { 
     $i++; 
     printf("%-30.30s",$method); 
     if($i%5==0) 
      echo "\n"; 
    } 
} 

这里有两个数组条目显示结构。请注意,播放列表中有一个videos关键字,其中包含一系列视频。测试videos密钥可以告诉你它是一个播放列表。 url是您的用户可以点击以在YouTube网站上打开视频或播放列表的内容。

[1] => Array 
    (
     [title] => Ducatitech.com "HowTo" Adjust your Valves 
     [id] => 970EC735D36A95E8 
     [time] => 855 
     [views] => 144847 
     [rating] => 4.9322033 
     [videos] => Array 
      (
       [dIj3nSJGPZw] => http://www.youtube.com/watch?v=dIj3nSJGPZw&feature=youtube_gdata_player 
       [3WQY1MRlmH4] => http://www.youtube.com/watch?v=3WQY1MRlmH4&feature=youtube_gdata_player 
      ) 

     [thumb] => http://i.ytimg.com/vi/dIj3nSJGPZw/mqdefault.jpg 
     [url] => http://www.youtube.com/watch?v=dIj3nSJGPZw&list=PL970EC735D36A95E8 
      ) 

     [thumb] => http://i.ytimg.com/vi/mcnIAErKc-g/mqdefault.jpg 
     [url] => http://www.youtube.com/watch?v=mcnIAErKc-g&list=PLEDADE9CA0E65BA78 
    ) 
[7] => Array 
    (
     [id] => 80yCiFkOB9g 
     [url] => http://www.youtube.com/watch?v=80yCiFkOB9g&feature=youtube_gdata_player 
     [title] => Ducatitech.com: ExactFit Timing Belt Tensile Test 
     [views] => 7589 
     [rating] => 4.25 
     [thumb] => http://i.ytimg.com/vi/80yCiFkOB9g/0.jpg 
     [time] => 625 
    ) 

最终,东西你们都从show_methods()的那种:

Methods for Zend_Gdata_YouTube_VideoEntry 

__construct     __get       __isset      __set       __toString      
__unset      addVideoDeveloperTag   delete      encode      ensureMediaGroupIsNotNull  
flushNamespaceLookupCache  getAlternateLink    getAuthor      getCategory     getComments     
getContent     getContributor    getControl     getDOM      getEditLink     
getEtag      getExtensionAttributes  getExtensionElements   getFeedLink     getFlashPlayerUrl    
getHttpClient     getId       getLicenseLink    getLink      getLocation     
getMajorProtocolVersion  getMediaGroup     getMediaSource    getMinorProtocolVersion  getNextLink     
getNoEmbed     getPreviousLink    getPublished     getRacy      getRating      
getRecorded     getRights      getSelfLink     getService     getSource      
getStatistics     getSummary     getText      getTitle      getTitleValue     
getUpdated     getVideoCategory    getVideoCommentFeedUrl  getVideoComplaintsLink  getVideoDescription   
getVideoDeveloperTags   getVideoDuration    getVideoGeoLocation   getVideoId     getVideoRatingInfo    
getVideoRatingsLink   getVideoRecorded    getVideoResponsesLink   getVideoState     getVideoTags     
getVideoThumbnails   getVideoTitle     getVideoViewCount    getVideoWatchPageUrl   getWhere      
getXML      isVideoEmbeddable    isVideoPrivate    lookupNamespace    registerAllNamespaces   
相关问题