我如何循环索引结果?使用PHP?

时间:2013-03-22 21:46:39

标签: php json

我得到了这个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>";
?>

我基本上希望start-index参数循环1,2,3等... Youtube API:https://developers.google.com/youtube/2.0/developers_guide_protocol_api_query_parameters

谢谢!

1 个答案:

答案 0 :(得分: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

使用Paging through results

尝试

https://gdata.youtube.com/feeds/api/playlists/$id?alt=jsonc&v=2&start-index=51&max-results=50;