【问题标题】:How To Get User Video From YouTube In A Paginated Way如何以分页方式从 YouTube 获取用户视频
【发布时间】:2014-09-28 02:05:16
【问题描述】:

我正在尝试获取我几乎已存档的频道视频,但是 YouTube 每页检索 50 个结果,而我的频道有 150 个视频,对结果进行分页会更整洁。可以用下面的代码做到这一点吗?如果是这样,请告诉我如何

我是一名 PHP 程序员,但对使用它并向您发送 http 请求(包括分页内容)完全陌生。

这是我目前所拥有的

<?php
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/gamertv273/uploads?start-index=1&max-results=10';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;

      ?>

<link rel='previous' type='application/atom+xml'
  href='http://gdata.youtube.com/feeds/api/users/gamertv273/uploads?start-index=1&max-results=10'/>
<link rel='next' type='application/atom+xml'
  href='http://gdata.youtube.com/feeds/api/users/gamertv273/uploads?start-index=2&max-results=10'/>
      <div class="videoitem">
        <div class="videothumb"><a href="<?php echo $watch; ?>" class="watchvideo"><img src="<?php echo $thumbnail;?>"
        alt="<?php echo $media->group->title; ?>" /></a></div>


        <div class="videotitle">
            <h3><a href="<?php echo $watch; ?>" class="watchvideo"><?php echo $media->group->title; ?></a></h3>

        </div>
      </div>      
<?php $i++; if($i==3) { echo '<div class="clear small_v_margin"></div>'; $i=0; } } ?>

【问题讨论】:

  • 设法获得一些基本的东西我看到你们都可以想出什么来帮助社区;)
  • 您的问题是什么?如果您想尝试它是否有效,请尝试执行它;-) 如果输出不是您所期望的返回这里,我们可以尝试搜索特定问题。但总的来说你的想法是正确的。
  • 我没有完整的分页,我认为如果不使用你需要学习的 you tube API 很难实现,但我的想法是一个你点击更多视频的分页。 . 但它会做;)

标签: php http video youtube pagination


【解决方案1】:

http://gdata.youtube.com/feeds/api/users/gamertv273/uploads?start-index=100&max-results=50

start-index 是视频开始的索引。

max-results 是返回的条目数(允许的最大值为 50)。

第 1 步

首先我建议您将 页码 解析到您自己的页面中,然后您的代码相应地查询 YTube。即

www.yourwebsite.com/your-page.php?page=1

在你的代码中:

<?php

$results_per_page = 20;
$start_index = $_GET['page'] - 1 * ($results_per_page + 1);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/gamertv273/uploads?start-index=' . $start_index . '&max-results=' . $results_per_page;
//[rest of the code]

?>

如果您的页面是 1,这将返回结果 0-20,如果您的页面是 2,则返回 20-40 等等。

第 2 步

然后,您需要输入页码。如果您有 126 个视频,那么应该有 7 个页面可用,每个页面有 20 个结果。要知道总共有多少个视频,请参考下面的 API:

http://gdata.youtube.com/feeds/api/users/gamertv273/uploads?v=2&alt=jsonc&max-results=0

您现在可以使用 json_decode() 解析 json 响应,获取 totalItems 并除以 $results_per_page 以获得可用页面的数量。然后,使用上一个/下一个按钮轻松打印每个页面的标签。

<a href="www.yourwebsite.com/your-page.php?page=1">previous page</a>
<a href="www.yourwebsite.com/your-page.php?page=3">next page</a>
<a href="www.yourwebsite.com/your-page.php?page=1">page 1</a>
<a href="www.yourwebsite.com/your-page.php?page=2">page 2</a>
<a href="www.yourwebsite.com/your-page.php?page=3">page 3</a>
<a href="www.yourwebsite.com/your-page.php?page=4">page 4</a>

【讨论】:

    猜你喜欢
    • 2016-02-29
    • 1970-01-01
    • 2019-05-22
    • 2019-09-14
    • 2011-03-03
    • 2015-09-03
    • 2021-09-07
    • 2011-03-03
    • 2015-03-06
    相关资源
    最近更新 更多