用逗号分享的youtube api播放列表结果,试图计算播放列表中所有视频的总和持续时间 [英] Stuck with paginating youtube api playlist results, trying to calculate tha sum duration of all the videos in a playlist

查看:143
本文介绍了用逗号分享的youtube api播放列表结果,试图计算播放列表中所有视频的总和持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的drupal网站制作一个小PHP代码片段,其中列出了YouTube播放列表中所有视频的所有持续时间。
我设法在这个网站找到一个很好的起点,我做了一些改变,几乎是好的:

I would like to make a little php snippet for my drupal site, which counts all the durations of all the videos in a youtube playlist. I managed to find a good starting point here at this site, I made some changes, and it is almost good:

<?php
$playlist_id = "266DBEDBE6892C11";
$url = "https://gdata.youtube.com/feeds/api/playlists/".$playlist_id."?v=2&alt=json&start-index=1&max-results=50";
$data = json_decode(file_get_contents($url),true);
$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);
$length = 0;

echo "Playlist Name: ".$info["title"]['$t'].'<br/>';
echo "Number of Videos (".$nVideo."):<br/>";
for($i=0;$i<200;$i++){
$temporary_length =  $video[$i]['media$group']['yt$duration']['seconds'];
$length += $temporary_length;
echo "Lenght: ". $temporary_length ."<br/>";
}

echo "Length: " . $length ;

?>

我的问题是,我不能分页,YouTube只给我最多50个结果。
我尝试使用start-index参数,但这对我来说没有效果。
我通过youtube api页面进行搜索,但我没有任何线索。我不是程序员,这是我可以想出的有限的编程知识。
我应该添加代码,计数播放列表中的所有视频?或者如果有人可以帮助我另一个片段,那也是完美的。

My problem is, that I can't paginate, youtube only gives me maximum 50 results. I tried with the start-index parameter, but that did not work for me. I search through the youtube api pages, but I have no clue hot to do it. I am no programmer, this is what I could come up with with my limited programming knowledge. What should I add to the code, to count all the videos in a playlist? Or If someone could help me with another snippet, that would be perfect also.

谢谢!

推荐答案

对不起,这里无法测试,但考虑到你收到什么错误,我想你首先需要检查你从YouTube上收到的数据。

Sorry can't test this here, but taking into account what error you are getting I think you first need to check what data you have receieved back from youtube.

我还提供了一种友好的方式来测试每页的当前页面和请求。

I also put in a friendly way for you to test the current page and requests per page.

<?php

//Configurable
$playlist_id = "266DBEDBE6892C11";
$results_per_request = 50;
$current_page = 1;

$start_index = $request_per_page * ($current_page - 1) + (($current_page > 1) ? 1 : 0);
$url = "https://gdata.youtube.com/feeds/api/playlists/".$playlist_id."?v=2&alt=json&start-index=".$start_index."&max-results=".$results_per_request;
$data = json_decode(file_get_contents($url),true);

if (is_array($data) && count($data) > 0)
{
$info = $data["feed"];
$video = $info["entry"];
$nVideo = count($video);
$length = 0;

echo "Playlist Name: ".$info["title"]['$t'].'<br/>';
echo "Number of Videos (".$nVideo."):<br/>";
for($i=0;$i<200;$i++){
$temporary_length =  $video[$i]['media$group']['yt$duration']['seconds'];
$length += $temporary_length;
echo "Lenght: ". $temporary_length ."<br/>";
}

echo "Length: " . $length ;
}
else
{
echo "Youtube did not return any more results."
}

?>

这篇关于用逗号分享的youtube api播放列表结果,试图计算播放列表中所有视频的总和持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆