看看这个链接,给出了一个例子
https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
&part=snippet,contentDetails,statistics,status
部分响应是
"contentDetails": {
"duration": "PT15M51S",
"aspectRatio": "RATIO_16_9"
},
现在我想检索内容详细信息或主要是持续时间。所以我打电话给使用
https://www.googleapis.com/youtube/v3/search?part=snippet,contentDetails&key=[API_KEY]&q=something&maxResults=15&&fields=items,nextPageToken,prevPageToken,tokenPagination
它显示
{
error: {
errors: [
{
domain: "youtube.part",
reason: "unknownPart",
message: "contentDetails",
locationType: "parameter",
location: "part"
}
],
code: 400,
message: "contentDetails"
}
}
为什么?我错过了什么?如何检索视频的持续时间?
正如您已经发现的那样,Search:list 调用不支持 part参数的 contentDetails。
可以包含在 Search:list 参数值中的部件名称是 id 和代码段,它们返回的数据很少。如果我们想在一个或多个视频上获得更具体的数据,我们应该使用搜索中的很少数据。
因此,要在进行搜索时获得视频持续时间,您必须拨打这样的电话
GET https://www.googleapis.com/youtube/v3/search?part=id&q=anything&key={YOUR_API_KEY}
并从响应项中提取视频 ID
"id": {
"kind": "youtube#video",
"videoId": "5hzgS9s-tE8"
}
并使用它来进行视频:列表调用以获取更具体的数据
https://www.googleapis.com/youtube/v3/videos?id=5hzgS9s-tE8&key=YOUR_API_KEY&part=snippet,contentDetails,statistics,status
并从响应数据中提取持续时间
"contentDetails": {
"duration": "PT15M51S",
"aspectRatio": "RATIO_16_9"
},
第 1 步:使用搜索列表视频 ID:列表
例如,您会收到 3 个 youtube 视频 ID,例如:
{ zOYW7FO9rzA, zOYW7FO9rzA, -vH2eZAM30s}
第2步: 您必须为第二次通话输入YouTube视频ID列表。
https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=zOYW7FO9rzA,zOYW7FO9rzA,-vH2eZAM30s&key={Your API KEY}
因此,您不必为每个视频
拨打电话结果将是:
{
"kind": "youtube#videoListResponse",
"etag": ""iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/p3KyUGr7ZRowLgKTqVFixrx7-mQ"",
"pageInfo": {
"totalResults": 3,
"resultsPerPage": 3
},
"items": [
{
"kind": "youtube#video",
"etag": ""iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/psAhg0bxv1n1IfKwXhrPMV223YE"",
"id": "zOYW7FO9rzA",
"contentDetails": {
"duration": "PT1M21S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": false
}
},
{
"kind": "youtube#video",
"etag": ""iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/YCi772AbPZizPuAFci702rE55tU"",
"id": "T3Ysb9O3EWI",
"contentDetails": {
"duration": "PT1H28M47S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": false
}
},
{
"kind": "youtube#video",
"etag": ""iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/2BnWErqkysQERsaRNyd1ffGgJes"",
"id": "-vH2eZAM30s",
"contentDetails": {
"duration": "PT12M57S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": false
}
}
]
}
格式持续时间:1H1M1S = 1 小时 & 1 分 & 1 秒
使用上面的链接,这里有一个快速的 php 示例,介绍如何只进行这两个调用,每次调用最多 50 个结果
$JSON = file_get_contents('https://www.googleapis.com/youtube/v3/search?part=snippet&q=cats&fields=items%2CnextPageToken%2CprevPageToken%2CtokenPagination&maxResults=50&key={YOUR_API_KEY});
上面的链接将搜索猫(q=cats)并将获取maxResults=50
。
接下来,我们将每个id存储在一个用逗号分隔的字符串中
$get_duration="";
foreach ($JSON_Data->items as $ids) {
$get_duration .=$ids->id->videoId.",";
}
$get_duration = rtrim($get_duration, ",");
最后,我们使用$get_duration
中包含的批处理 ID 进行第二次调用,并显示每个视频的标题和持续时间
$JSON= file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics%2Cstatus&id='.$get_duration.'&key={YOUR_API_KEY}');
$JSON_Data = json_decode($JSON);
foreach ($JSON_Data->items as $ids) {
$date = new DateTime('1970-01-01');
$date->add(new DateInterval($ids->contentDetails->duration));
echo "Title: ".$ids->snippet->title."nDuration: {$date->format('H:i:s')}nn";
}
结果将是这样的
> Title: Cats Being Jerks Video Compilation || FailArmy
> Duration: 00:08:33
>
> Title: Ultimate cat vines compilation - Best cat vines 2014 / 2015
> Duration: 00:14:58
>
> Title: Funny cats annoying owners - Cute cat compilation
> Duration: 00:05:58
>
> Title: Funny Cats Compilation 60 min - NEW in HD 2014
> Duration: 00:57:51