我正在运行一个php脚本,该脚本使用channelId从YouTube频道检索视频。
$API_key = 'MY_KEY';
$channelID = 'MY_CHANNEL_ID';
$maxResults = 2;
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
然后我将其插入网页中:
<iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
<h2>'. $item->snippet->title .'</h2>
<h2>'. $item->snippet->publishedAt .'</h2>
<h2>'. $item->statistics->duration .'</h2>
我似乎无法访问json文件的统计信息部分。当我打印json时,它看起来像这样:
stdClass Object
(
[kind] => youtube#searchListResponse
[etag] => "XI7nbFXulYBIpL0ayR_gDh3eu1k/n6hG3LC-2onr-
5PwKJtRxnC1Kjc"
[nextPageToken] => CAEQAA
[regionCode] => US
[pageInfo] => stdClass Object
(
[totalResults] => 1094
[resultsPerPage] => 1
)
[items] => Array
(
[0] => stdClass Object
(
[kind] => youtube#searchResult
[etag] =>
"XI7nbFXulYBIpL0ayR_gDh3eu1k/lKQ8CNf00_OxqchddzGEYtbidqk"
[id] => stdClass Object
(
[kind] => youtube#video
[videoId] => YxjEmz6KWdU
)
[snippet] => stdClass Object
(
[publishedAt] => 2018-04-06T21:10:48.000Z
[channelId] => UCV4xOVpbcV8SdueDCOxLXtQ
[title] => SE ACABO :'V
[description] => https://www.twitch.tv/fernanfloo.
[thumbnails] => stdClass Object
(
[default] => stdClass Object
(
[url] => https://i.ytimg.com/vi/YxjEmz6KWdU/default.jpg
[width] => 120
[height] => 90
)
[medium] => stdClass Object
(
[url] => https://i.ytimg.com/vi/YxjEmz6KWdU/mqdefault.jpg
[width] => 320
[height] => 180
)
[high] => stdClass Object
(
[url] => https://i.ytimg.com/vi/YxjEmz6KWdU/hqdefault.jpg
[width] => 480
[height] => 360
)
)
[channelTitle] => Fernanfloo
[liveBroadcastContent] => none
)
)
)
)
因此,显然json文件并未包含所有信息。我认为这是网址问题。它只是创建带有一些信息的json文件。我尝试过将网址和其他路径切换为持续时间。
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
我是YouTube Data API的新手,而且我的知识不完整。有人可以解释我在做什么错吗?
答案 0 :(得分:0)
您需要修改API请求,以在statistics
参数中添加part
。
此外,您正在使用“搜索”选项-根据其documentation,它不会显示statistics
值(这是您所需要的)。
因此,您必须使用“ videos”选项。
这是您可以使用的修改后的API请求:
https://www.googleapis.com/youtube/v3/videos?id=YxjEmz6KWdU&maxResults=2&part=snippet%2Cstatistics&key=<YOUR_API_KEY>
以下信息摘自YouTube Data API - official documentation:
statistics
的YouTube视频的ID。snippet
和 statistics
。该视频的统计信息将显示在API响应的statistics
部分。
以下是使用try-it functionality并指定videoId YxjEmz6KWdU
的结果:
{
"kind": "youtube#videoListResponse",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/4N2LykLhXBiFo79x71IYUazfgdk\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [{
"kind": "youtube#video",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/Pvuq35nnqnUa3-pETVOKpdemDVU\"",
"id": "YxjEmz6KWdU",
"snippet": {
"publishedAt": "2018-04-06T21:10:48.000Z",
"channelId": "UCV4xOVpbcV8SdueDCOxLXtQ",
"title": "SE ACABO :'V",
"description": "→ https://www.twitch.tv/fernanfloo",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/YxjEmz6KWdU/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/YxjEmz6KWdU/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/YxjEmz6KWdU/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/YxjEmz6KWdU/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/YxjEmz6KWdU/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "Fernanfloo",
"tags": [
"fernanfloo",
"fernan",
"fernan el crack",
"twitch",
"fernanfloo twitch",
"en vivo",
"en directo",
"regreso",
"el regreso de fernanfloo",
"vlog",
"blog",
"2018",
"pc",
"fornite",
"fernanfloo con barba",
"fernanfllo viejo",
"lol"
],
"categoryId": "20",
"liveBroadcastContent": "none",
"localized": {
"title": "SE ACABO :'V",
"description": "→ https://www.twitch.tv/fernanfloo"
}
},
"statistics": {
"viewCount": "21926458",
"likeCount": "1371385",
"dislikeCount": "135852",
"favoriteCount": "0",
"commentCount": "303938"
}
}]
}