1

我得到了这个 php 代码,它从 youtube 的 api 获取播放列表并显示视频,问题是我每个请求只能显示 25 个视频如何让我的 php 脚本有一个“加载更多”链接,该链接将请求另外 25 个视频,要么它可以重新加载页面或在同一页面中附加其他结果,我只需要指向正确的方向

我的代码:

<?php
    $id = urlencode($_GET['id']);
    $url = "https://gdata.youtube.com/feeds/api/playlists/$id?alt=jsonc&v=2&start-index=1
&max-results=25";
    $content = file_get_contents($url);
    $json = json_decode($content, true);
    echo "<div id=\"info\"><center><h1><b>{$json['data']['title']}</center></b></h1><br>{$json['data']['description']} there are {$json['data']['totalItems']} uploaded in this playlist.</center><p></div><div class=\"decoration\"></div>";
    echo "<div id=\"center\">";
    echo "<ul>";
    $count = 0;
    foreach ($json['data']['items'] as $items) {
        ++$count;
        echo "<li style=\"width: 300px;min-height: auto;border: none;display: inline-block;margin: 5px;padding:10px;background-color: rgba(17,0,52,0.14);border-radius: 5px;\"><a href=\"video.php?plid={$json['data']['id']}&vidid={$items['video']['id']}\"><font size=\"5\" style=\"font-weight:bold;\">{$items['video']['title']}</font></a><Br \>";
        echo "<a href=\"video.php?plid={$json['data']['id']}&vidid={$items['video']['id']}\"><img style=\"width:300;height:auto;\" src=\"{$items['video']['thumbnail']['hqDefault']}\" title=\"{$items['video']['title']}\" id=\"ytThumb\"></img></a></li>";
    }
echo "</ul>";
echo "</div>";
?>

我基本上希望开始索引参数循环 1、2、3 等... Youtube API:https ://developers.google.com/youtube/2.0/developers_guide_protocol_api_query_parameters

谢谢!

4

1 回答 1

1

如果您运行,您将只允许max 50使用每个请求的结果max-results

$id = "UUpsSadsgX_Qk9i6i_bJoUwQ"; // for testing
$url = "https://gdata.youtube.com/feeds/api/playlists/$id?alt=jsonc&v=2&max-results=50";
$content = file_get_contents($url);
$json = json_decode($content, true);

echo "<div id=\"info\"><center><h1><b>{$json['data']['title']}</center></b></h1><br>{$json['data']['description']} there are {$json['data']['totalItems']} uploaded in this playlist.</center><p></div><div class=\"decoration\"></div>";
echo "<div id=\"center\">";
echo "<ul>";
$count = 0;
foreach ( $json['data']['items'] as $items ) {
    ++ $count;
    echo "<li style=\"width: 300px;min-height: auto;border: none;display: inline-block;margin: 5px;padding:10px;background-color: rgba(17,0,52,0.14);border-radius: 5px;\"><a href=\"video.php?plid={$json['data']['id']}&vidid={$items['video']['id']}\"><font size=\"5\" style=\"font-weight:bold;\">{$items['video']['title']}</font></a><Br \>";
    echo "<a href=\"video.php?plid={$json['data']['id']}&vidid={$items['video']['id']}\"><img style=\"width:300;height:auto;\" src=\"{$items['video']['thumbnail']['hqDefault']}\" title=\"{$items['video']['title']}\" id=\"ytThumb\"></img></a></li>";
}
echo "</ul>";
echo "</div>";


// you would get 50 results 

要获得更多结果,您需要 结合使用start-indexmax-results

尝试

https://gdata.youtube.com/feeds/api/playlists/$id?alt=jsonc&v=2&start-index=51&max-results=50;
于 2013-03-22T22:00:57.277 回答