【问题标题】:Youtube api playlist idYoutube api 播放列表 ID
【发布时间】:2012-02-20 16:49:32
【问题描述】:

我正在使用@http://code.google.com/apis/youtube/2.0/developers_guide_php.html找到的测试代码来创建播放列表:

$newPlaylist = $yt->newPlaylistListEntry();
$newPlaylist->summary = $yt->newDescription()->setText($desc);
$newPlaylist->title = $yt->newTitle()->setText($title);
// post the new playlist
$postLocation = 'http://gdata.youtube.com/feeds/api/users/default/playlists';
try {
  $playlist = $yt->insertEntry($newPlaylist, $postLocation);
  } 
catch (Zend_Gdata_App_Exception $e) {
  echo $e->getMessage();
}

播放列表已创建,但如何获取刚刚创建的播放列表的 id 或 url?

【问题讨论】:

    标签: php api youtube


    【解决方案1】:

    我也有同样的问题。我已经设法更进一步,但我仍然无法获得播放列表 ID。这是我所做的:

    代替:

    $playlist = $yt->insertEntry($newPlaylist, $postLocation);
    

    我用过:

    $playlist = $yt->insertEntry($newPlaylist, $postLocation, 'Zend_Gdata_YouTube_PlaylistListEntry');
    

    但是当我尝试通过$playlist->getPlaylistID()$playlist->playlistId->text 获取ID 时,我得到了同样的异常:

    2 之前的版本不支持 yt:playlistId 元素。

    即使我之前用$yt->setMajorProtocolVersion(2); 设置了它

    【讨论】:

      【解决方案2】:

      这是一个完整而彻底的 hack,我遇到了同样的问题,所以我进入了 Zend/Gdata/YouTube/PlaylistListEntry.php 的第 229 行的课程,我注释掉了 if else 语句。

       /**
       * Returns the Id relating to the playlist.
       *
       * @throws Zend_Gdata_App_VersionException
       * @return Zend_Gdata_YouTube_Extension_PlaylistId  The id of this playlist.
       */
      public function getPlaylistId()
      {
          /*if (($this->getMajorProtocolVersion() == null) ||
              ($this->getMajorProtocolVersion() == 1)) {
              require_once 'Zend/Gdata/App/VersionException.php';
              throw new Zend_Gdata_App_VersionException('The yt:playlistId ' .
                  'element is not supported in versions earlier than 2.');
          } else {*/
              return $this->_playlistId;
          //}
      }
      

      我很想有人向我们展示如何以正确的方式解决这个问题,但这让它变得如此

      function printPlaylistListEntry($playlistListEntry, $showPlaylistContents = false)
          {
      
            $this->yt->setMajorProtocolVersion(2);
            echo '<br>Title: ' . $playlistListEntry->title->text . "\n";
            echo '<br>Description: ' . $playlistListEntry->description->text . "\n";
            echo '<br>playlistId: ' . $playlistListEntry->playlistId->text . "\n";
      

      ...(来自 youtube v2 php api)。

      将返回播放列表 ID。

      标题:正确的标题

      描述:正确的描述

      playlistId:正确的playlistId

      编辑:我认为这可能是一个更好的解决方案:

      if ($this->getMajorProtocolVersion() < 2) {
              require_once 'Zend/Gdata/App/VersionException.php';
              throw new Zend_Gdata_App_VersionException('The yt:playlistId ' .
                  'element is not supported in versions earlier than 2.');
          } else {
      
              return $this->_playlistId;
          }
      

      将 getPlaylistId() 函数替换为 this,遵循前面的 getDescription 函数的逻辑,并且它的 hacky 更少。再次完全接受来自 zend 人的关于为什么这是或不是一个好主意的批评。

      【讨论】:

        【解决方案3】:

        您不需要任何特殊的技巧来完成这项工作。您只需要显式设置 $playlist 变量的协议版本以及 $yt 变量。如您所说,为 $yt 设置主要协议版本:

        $yt->setMajorProtocolVersion(2);
        

        然后在你初始化 $playlist 之后,也设置协议:

        $playlist = $yt->insertEntry($newPlaylist, $postLocation, 'Zend_Gdata_YouTube_PlaylistListEntry');
        $playlist->setMajorProtocolVersion(2);
        

        一旦你这样做了,你应该能够得到你的播放列表 ID 没有问题:)

        $playlist_id = $playlist->getPlaylistID();
        

        【讨论】:

          猜你喜欢
          • 2013-02-16
          • 2015-10-18
          • 2018-02-19
          • 2021-12-21
          • 2015-01-06
          • 2013-07-08
          • 2015-07-05
          • 2011-07-04
          • 2013-04-07
          相关资源
          最近更新 更多